8 replies
by tclere
You can add this code to your workbook. It will put all of the nonessential zeros back into your Octets. Then you can sort it and get the results you are looking for. This sample assumes that the IP addresses are in column A and there is no other data. You can change the code to suit your needs.
[code]
Option Explicit
Option Base 1
Public Sub FixData()
Dim sTmp As String, iRow As Long, iPos As Integer
Dim i(4) As Integer
iRow = 1
Do While Len(Range("A" & iRow).Text) >0
sTmp = Range("A" & iRow).Text
'Get the first Octet
iPos = InStr(1, sTmp, ".")
i(1) = CInt(Left(sTmp, iPos - 1))
sTmp = Right(sTmp, Len(sTmp) - iPos)
'Get the second Octet
iPos = InStr(1, sTmp, ".")
i(2) = CInt(Left(sTmp, iPos - 1))
sTmp = Right(sTmp, Len(sTmp) - iPos)
'Get the third Octet
iPos = InStr(1, sTmp, ".")
i(3) = CInt(Left(sTmp, iPos - 1))
sTmp = Right(sTmp, Len(sTmp) - iPos)
'Get the last Octet
i(4) = CInt(sTmp)
'Rewrite the string with all of the zeros
sTmp = Format(i(1), "000") & "." & _
Format(i(2), "000") & "." & _
Format(i(3), "000") & "." &_
Format(i(4), "000")
Range("A" & iRow).FormulaR1C1 = sTmp
iRow = iRow + 1
Loop
Columns("A:A").Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1,MatchCase:=False, Orientation:=xlTopToBottom
Range("A1").Select
End Sub
[/code]
by rick
by mservino
another thing you can do, without all the code is to just insert 4 columns on your worksheet, copy from your IP list into the first new column, use DATA --> Text to Columns, expand on a delimiter of ".". Then just select the 4 columns Format Cells,as numbers, General, then do a sort on the columns. End of story, if you don't want to see the columns your sorting on just hide them.
-Mike
by rick
by rick
by mservino
by rick
Thanks for your help. I realized that I was sorting on the 4th octet only. Once I sorted on 3 of the octets I got the results I was looking for.
-Rick
by rick
This discussion is closed to new replies.
Trending Software posts
Sort by Trending