View Single Post
  #4   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default Sorting IP Address

On Wed, 19 Oct 2005 07:00:07 -0700, "ggant"
wrote:

How do I sort fields that contain IP numbers in order? ex. Field A -
192.168.1.0.......192.168.1.121.....192.168.1.15. ...192.168.2.12....192.168.2.20...192.168.3.50

How can I sort these by the 3rd and 4th octives?


Here is a VBA macro that should sort IP addresses.

It requires that the IP addresses be in a column (but it does not check for
that.

To enter this, <alt<F11 opens the VB Editor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code below into the window that opens.

To use the code, first select the column of IP addresses you wish to sort.
Then <alt<F8 opens the Macro dialog box. Select SortIP and RUN.

Backup your data first, just in case.

=========================
Sub sortIP() 'sorts IP addresses
Dim i As Long, j As Long
Dim IP
Dim rg()

ReDim rg(Selection.Count - 1, 1)

For i = 0 To UBound(rg)
rg(i, 0) = Selection.Cells(i + 1, 1).Text
IP = Split(rg(i, 0), ".")
For j = 0 To 3
rg(i, 1) = rg(i, 1) & Right("0" & Hex(IP(j)), 2)
Next j
rg(i, 1) = hex2dec(rg(i, 1))
Next i

rg = BubbleSort(rg, 1)

For i = 0 To UBound(rg)
Selection.Cells(i + 1, 1) = rg(i, 0)
Next i

End Sub
======================
--ron