MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Sorting IP Addresses


November 01, 2001 - by Bill Jelen

Russell has a column of internal IP addresses and he want to sort them. The problem is that the .100. addresses are coming before the .22. addresses.

If Russell adds a new, temporary column to be used for sorting, he can use this new custom function IPForSort(). This code should be pasted into a code module in the workbook.

Public Function IPforSort(OrigVal)
    IPforSort = ""
    OrigVal = OrigVal & "."
    Bg = 1
    For i = 1 To Len(OrigVal)
        If Mid(OrigVal, i, 1) = "." Then
            IPforSort = IPforSort & Format(Mid(OrigVal, Bg, i - Bg), "000") & "."
            Bg = i + 1
        End If
    Next i
    IPforSort = Left(IPforSort, Len(IPforSort) - 1)
End Function