Stripping/ Splitting up Parts of IP addresses
Coding
Evaluate(Range) solution
Because I am currently still wanting to have some easily see-able spreadsheet lists, then this could be something that may be a nice neat use of what I Call Evaluate(Range) solutions. These basically work on an existing spreadsheet List, but from within VBA, and can give results knocking the socks off of what is still considered more normal most efficient professional ways. Me and a few others have perfected them in recent years. (They works best on older more stable and reliable Office versions also, - as with most things Microsoft, they have been slowly messing things up over the last 10-20 years)
The idea is to develop a formula in a spreadsheet, preferably a fairly simple one, and then we use it in VBA to very efficiently work on the spreadsheet to paste in all the final results, only results – No formulas will appear in the spreadsheet.
The uploaded file shows development of one possible solution, and the following explanations discuss that development and final answer. There are probably lots of possible solutions, maybe some better. But this one will probably do for now
The formula development
A good start point is the Excel SUBSTITUTE function in this in the following sort of form, which is a form using all the arguments, including the last, which is optional.
=SUBSTITUTE("43.134.41.39",".","_",2)
The SUBSTITUTE function is one of a few Excel functions doing some helpful things that do not have a direct equivalent in native VBA. The feature I find very useful with the SUBSTITUTE function is the ability to replace a particular occurrence of a character or character set. (A close equivalent in VBA, the Replace function almost does the same – it has the option to replace just a certain number of occurrences so could be fiddled to do effectively the same)
So, in the way I am using the SUBSTITUTE function above, it effectively puts a _ where the second . was,
, the idea after that being to FIND the poison of that single _ , after which we can take just the LEFT up to the _
This is a good start point in our case, since the 2 in the SUBSTITUTE has put the _ at the place of the second . , so I can FIND it , then take the LEFT up to it to get 43.134 , but thereafter, in any final formula, if I want to do similar to get, for example 43.134.41, then I simply need to change the 2 to a 3 so as to be dealing with the third .
In the uploaded file I go through a typical formula development. I tend to start with a typical data in the first cell then develop formula in the columns to the right.
https://postimg.cc/T5J1zVbs
https://i.postimg.cc/KjdTgQ8m/IPaddr...ts-Formula.jpg
Code:
=SUBSTITUTE(A2;".";"_";2) =FIND("_";B2) =LEFT(A2;C2-1) =LEFT(A2;FIND("_";B2)-1) =LEFT(A2;FIND("_";SUBSTITUTE(A2;".";"_";2))-1)
In simple wording what I do is: get the information, or make the changes I need to, using a formula in a cell in the columns, and then when I am finally at a working formula, (which will be at this point typically using the cells in the columns), I work backwards as it were, substituting into the column cell range the actual formula in that referenced column cell, the final goal being to have a formula only needing the original data.
It is difficult to easily explain that in words, or if it is, then it is probably quicker to take a look at, and fiddle a bit with, the cells, and you then should quickly see what is going on.
But here is a quick explanation anyway, taking the first data in cell A2,
43.134.41.39
In B2 I change the second . to a _
In C2 I find position along of that _
I now have the information I need to use the LEFT function to take just the left part of the data that I want. So at this point I have the working formula, but it has cell references in it. I don't want that, or rather I do, but only the original data reference, in this case A2
From then on I replace cell references in the formula with the actual formula in the referenced cell. I keep doing this until I finally only am referencing the actual data cell, A2, in this case
The VBA coding, ( for Evaluate(Range) solution )
This is usually straight forward, only occasionally requiring some tweaking.
In principal, in the simplest application of the Evaluate(Range) idea, the idea is the basic idea or way that The VBA Evaluate(" ") works:
If we put a formula/function that is working in a cell into a code line here, like this
___________ = Evaluate("here")
, then the result returned in the coding is the same result that would usually be returned to the cell(s).
I say cell(S), because for stable older Office versions, Excel was a well thought out, potentially very efficient tabulation calculation software: Typical computer innards workings have some efficient synchronisation to the screen interface us humans see and use. Using just a single cell puts something of a restriction on this process. However, as a good compromise, the original clever Excel developers came up with a system that allows minimum disturbance, a sort of block of vision across all but the used cell, which can however be removed easily. This is related to the so called CSE "array" formulas.
How this effects things in the Evaluate(" ") was not so clearly looked at. Possibly it was not envisaged that people like Rick Rothstein and me would take an interest and come up with some rather novel solutions.
The bottom line is that we can usually get the Evaluate(" ") to return us an array of results for workings over a rectangular range, rather than a single cell, in a similar way to that which the CSE keyboard related actions can.
Before we go any further with this explanation, it might help us keep a clear head by
making a start on the final solution….
This might be the typical development once I have a working solution in the Spreadsheet……
_ Here is a working formula, as developed in the uploaded file and discussed above
=LEFT(A2,FIND("_",SUBSTITUTE(A2,".","_",2))-1)
That formula takes in A2, the value 43.134.41.39, and gives us 43.134
_ We can almost simply put that inside the Evaluate("here")
But this will error, Evaluate("LEFT(A2,FIND("_",SUBSTITUTE(A2,". ","_",2))-1)")
The snag is that the two outermost quotes, Evaluate(" _____ ") , are acting as an enclosing quote pair in VBA to tell VBA that a literal string is there rather than some coding. So if we simply put the working formula in, then we will get some messy errors as VBA will get a bit confused with all those extra quotes in different places.
We find that in general doubling the quotes, or rather a pair of quotes with no space between them, in a VBA string, seems to make VBA "see" just a single quote in the final string, which is what we want. This is not documented in detail. My guess, or rather a simple Layman way of thinking about it that helps me "get it", is to think that VBA in a code line is seeing something like this
_________ " "" "
, resulting in what is syntaxly OK - two lots of enclosing quotes somehow intimately entwined, and looking from/ in either of them, it sees simultaneously at the same position a quote. – The Purple quote enclosed in the Blue quotes appears at the end of that string, and by virtue of the merged/intertwining, the blue quote which is at the start of the enclosing purple quotes is "seen" at the same place by VBA. The word "seen" in this respect could be though of where VBA holds or places or references the character as the code is executed. Whatever it is doing, it is doing it twice, when once would have done, but the end effect is the same – like writing a character twice in the same place – finally we just have one character.
So, in simple terms, if you want a quote, somewhere in a string in VBA, then put two quotes there
A very nice and easy way to check you have arranged that VBA sees the formula you want is to get VBA to tell you what it sees.
The Immediate window can be useful for this, as it is for so much in VBA development. This code line will tell you what is seen by VBA:
Debug.Print "LEFT(A2,FIND(""_"",SUBSTITUTE(A2,""."",""_"", 2))-1)"
That would give us printed in the Immediate Window
LEFT(A2,FIND("_",SUBSTITUTE(A2,".","_",2))-1)
https://i.postimg.cc/Yqkd7nDj/Debug-...ate(Range).jpg
https://i.postimg.cc/Yqkd7nDj/Debug-...ate(Range).jpg
So all looks OK, and indeed, if as example we had in cell A2
43.134.41.39
, and then this code line was executed,
Let Range("I2") = Evaluate("LEFT(A2,FIND(""_"",SUBSTITUTE(A2,"".""," "_"",2))-1)")
, we would get in cell I2
43.134
Similarly, this
Let Range("I2") = Evaluate("LEFT(A2,FIND(""_"",SUBSTITUTE(A2,"".""," "_"",3))-1)")
, we would get in cell I2
43.134.41
So we can use a working spreadsheet formula in VBA to get the result in a spreadsheet, without having the formula itself in the spreadsheet.
We will deal with getting a range of results in on go into a spreadsheet in the next post