Sorting Excel column by IP address
As nixda mentioned in the comments, helper columns will make this possible. You have two options for maintaining the sheet afterward:
- Add all new IPs in the split helper columns.
- Repeat the Text-to-columns procedure for new additions.
Here's the procedure:
Select your IP column and click
Data
>Text-to-Columns
Choose the Delimted option and click Next. Check the
Other
checkbox and enter a period.
. Click Next.Keep all of the columns, keep them as General, click the range icon to edit the
Destination
section.Select the columns where you want the new text to appear. Hit the Enter key.
Make sure your range is selected and click
Data
>Sort
. Enter the sorting criteria. Keep adding levels for each octet.Here's the end result:
I realize this is an old post, but in the interest of providing a working solution, I present the following.
Just put this formula in an adjacent cell and update the references to point to the cell containing your IP address (A1 in this example). This will produce a result similar to 010.121.008.030, which can then be sorted (correctly) alphabetically. Then set the new column's width to zero and voila. Time to enjoy a cup of coffee.
=TEXT(MID(A1,1,FIND(".",A1)),"000")&"."&TEXT(MID(A1,FIND(".",A1)+1,FIND(".",A1,FIND(".",A1)+1)-1-FIND(".",A1)),"000")&"."&TEXT(MID(A1,FIND(".",A1,FIND(".",A1)+1)+1,FIND(".",A1,FIND(".",A1,FIND(".",A1)+1)+1)-1-FIND(".",A1,FIND(".",A1)+1)),"000")&"."&TEXT(MID(A1,FIND(".",A1,FIND(".",A1,FIND(".",A1)+1)+1)+1,LEN(A1)),"000")
In addition to John Homer's answer, I here provide a formula that also works in Apple's Numbers application, as TEXT
function does not exist there. Also tested in Google Docs.
=DEC2HEX(LEFT(B2,FIND(".",B2)-1),2)
&
DEC2HEX(MID(
B2,
FIND(".",B2)+1,
FIND(
".",
B2,
FIND(".",B2)+1
) - FIND(".",B2) - 1
),2)
&
DEC2HEX(MID(
B2,
FIND(
".",
B2,
FIND(".",B2)+1
) + 1,
FIND(
".",
B2,
FIND(
".",
B2,
FIND(".",B2)+1
)+1
) - FIND(
".",
B2,
FIND(".",B2)+1
) - 1
),2)
&
DEC2HEX(RIGHT(
B2,
LEN(B2) - FIND(
".",
B2,
FIND(
".",
B2,
FIND(".",B2)+1
)+1
)
),2)
Sub IPSplit()
HeaderRow = 1
ColimnName = "A"
BeginIPaddsressData = 2
Dim HeaderArray As Variant
HeaderArray = Array("IP oct 1", "IP oct 2", "IP oct 3", "IP oct 4")
Dim Octet() As String
Dim RangeSearch As Range, RangeFound As Range, LastCell As Range
Dim LastCellRowNumber As Long, LastCellColumnNumber As Long, RowNumber As Long
With ActiveSheet
Set LastCell = .Cells(HeaderRow, .Columns.Count).End(xlToLeft)
LastHeaderColumnNumber = LastCell.Column
Set RangeSearch = Range("1:1")
Set RangeFound = RangeSearch.Find(What:=HeaderArray(0), LookIn:=xlValues)
If RangeFound Is Nothing Then
RowNumber = 2
If .Cells(RowNumber, .Columns.Count) <> vbNullString Then
Set LastCell = .Cells(RowNumber, .Columns.Count)
LastCellColumnNumber = LastCell.Column
Else
Set LastCell = .Cells(RowNumber, .Columns.Count).End(xlToLeft)
'Specifies the last column LastCellColumnNumber.
LastCellColumnNumber = LastCell.Column
End If
Range(Cells(HeaderRow, LastCellColumnNumber + 1), Cells(HeaderRow, LastCellColumnNumber + 4)).Value = HeaderArray
'Insert Header
Else
LastCellColumnNumber = RangeFound.Column - 1
End If
Set LastCell = .Cells(.Rows.Count, ColimnName).End(xlUp)
'Specifies the last cell number in the column ColimnName.
LastCellRowNumber = LastCell.Row
End With
For I = BeginIPaddsressData To LastCellRowNumber
Octet = Split(Cells(I, ColimnName).Value, ".")
For O = 0 To 3
'cells populate the values of octets 1-4.
If (UBound(Octet) - O) >= 0 Then
Cells(I, ColimnName).Offset(0, LastCellColumnNumber + O).Value = Octet(O)
End If
Next
Next
End Sub