excelexcel-formulaformulaexcel-2013

Separate octets of IP address using formulas


I want to separate out the octets of an IP address using Formulas.

I have tried some things like substitute & find but cannot figure this out.

Example of what I want to achieve, by only starting with Cell A1 and Cell B1:

10.17.9.192 | 192.168.0.1
10          | 192
17          | 168
9           | 0
192         | 1

Solution

  • Here are four formulas you need

    A2:=LEFT(A1,FIND(".",A1)-1)
    A3:=MID(A1,FIND(".",A1)+1,FIND(".",A1,FIND(".",A1)+1)-FIND(".",A1)-1)
    A4:=MID(A1,FIND(".",A1,FIND(".",A1)+1)+1,FIND(".",A1,FIND(".",A1,FIND(".",A1)+1)+1)-(FIND(".",A1,FIND(".",A1)+1)+1))
    A5:=MID(A1,FIND(".",A1,FIND(".",A1,FIND(".",A1)+1)+1)+1,LEN(A1)-FIND(".",A1,FIND(".",A1,FIND(".",A1)+1)+1))
    

    The FIND function has a third argument named start. So to find the second decimal point, you FIND a decimal point but you start where the first decimal point is plus one. It would be like this

    =FIND(".",A1,4)
    

    You start in 4 because your first decimal point is in position 3. But you don't know that, so you have to compute the '4'.

    =FIND(".",A1,FIND(".",A1)+1)
    

    Now to get 4, we find the first decimal and add 1. That's not too bad to find the second. But to find the third, you have to go another level. And the fourth is yet another level. It's hard to read and hard to maintain.

    To make it a little easier, you can use helper columns.

    A2        =LEFT(A1,C2-1)
    C2        =FIND(".",A1)
    A3        =MID(A1,C2+1,C3-C2-1)
    C3        =FIND(".",A1,C2+1)
    A4        =MID(A1,C3+1,C4-C3-1)
    C4        =FIND(".",A1,C3+1)
    A5        =MID(A1,C4+1,LEN(A1)-C4-1)
    

    That way you're doing your FINDs in C and referring to those numbers in A.

    If you don't like helper columns, and I don't, you could write a UDF like

    Public Function FINDi(find_text As String, within_text As String, Optional instance As Long) As Long
    
        Dim lReturn As Long
        Dim i As Long
    
        Const lFINDFIRST As Long = 0
    
        If instance = lFINDFIRST Then
            lReturn = InStr(1, within_text, find_text)
        ElseIf instance < lFINDFIRST Then 'negative numbers finds last
            lReturn = InStrRev(within_text, find_text)
        Else
            lReturn = 0
            For i = 1 To instance
                lReturn = InStr(lReturn + 1, within_text, find_text)
            Next i
        End If
    
        FINDi = lReturn
    
    End Function
    

    and that gives you formulas like this

    A2        =LEFT(A1,findi(".",A1)-1)
    A3        =MID(A1,findi(".",A1)+1,findi(".",A1,2)-findi(".",A1,1)-1)
    A4        =MID(A1,findi(".",A1,2)+1,findi(".",A1,3)-findi(".",A1,2)-1)
    A5        =MID(A1,findi(".",A1,3)+1,LEN(A1)-findi(".",A1,3)-1)
    

    Not as clean as the helper column, but self contained and definitely better than the built-in FIND.

    Another UDF you could write duplicates what VBA's split function does.

    Public Function SplitString(ByVal sInput As String, ByVal sDelim As String, ByVal lWhich As Long) As String
    
        SplitString = Split(sInput, sDelim)(lWhich - 1)
    
    End Function
    

    That formula looks like

    A2        =SplitString($A$1,".",ROW()-1)
    A3        =SplitString($A$1,".",ROW()-1)
    A4        =SplitString($A$1,".",ROW()-1)
    A5        =SplitString($A$1,".",ROW()-1)