+ Reply to Thread
Results 1 to 4 of 4

Changing 3rd octet in an IP address

Hybrid View

  1. #1
    Registered User
    Join Date
    01-21-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    13

    Question Changing 3rd octet in an IP address

    Greetings from a newbie.

    I have a need to be able to separate the 3rd octet in an IP address so I can increment it by one and then change the last octet. This is for automatically setting a default gateway for a /23 subnet mask.
    For example...

    10.2.16.2 would become 10.2.17.254
    192.168.10.2 would become 192.168.11.254

    Octets may be made up of 1-3 digits.

    Any help is appreciated.

    ReeceB...

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Changing 3rd octet in an IP address

    With your sample data in A1:A2
    A1: 10.2.16.2
    A2: 192.168.10.2

    This regular formula transforms that string by:
    ...incrementing the 3rd section by 1
    ...replacing the 4th section with 254
    B1: =SUBSTITUTE(LEFT(SUBSTITUTE(A1,".","."&REPT(" ",LEN(A1))),LEN(A1)*2)&MID(SUBSTITUTE(A1,".","."&REPT(" ",LEN(A1))),LEN(A1)*2,LEN(A1))+1&".254"," ","")
    Copy that formula into B2

    The end results will be:
    10.2.17.254
    192.168.11.254


    Is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Changing 3rd octet in an IP address

    I did a similar task in the past, I think for my take I'd probably go with VBA.

    Public Function UpdateIP(ByVal strIP As String) As String
    Dim i As Integer, x As Integer
    Dim strLeft As String, strMid As String
    
    x = 0
    For i = 1 To Len(strIP)
        If Mid(strIP, i, 1) = "." Then
            x = x + 1
            If x = 2 Then
                strLeft = Left(strIP, i)
            ElseIf x = 3 Then
                strMid = Mid(strIP, Len(strLeft) + 1, i - Len(strLeft) - 1)
                strMid = strMid + 1
            End If
        End If
    Next i
    
    UpdateIP = strLeft & strMid & ".254"
        
    End Function
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    01-21-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Changing 3rd octet in an IP address

    Ron, you are an absolute champion.

    Even a simple person like me can work with that. Works a treat.

    Thanks heaps for your quick response. It has saved what little hair I have left.

    Reece...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1