Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to quickly sort IP address from low to high in Excel?

In general, we use the Sort function to sort string in Excel. But if there are some IP addresses needed to sort, the sorting order may be wrong by using the Sort function directly as below screenshot shown. Now I have some ways to sort the IP addresses quickly and correctly in Excel.

Wrong Sort by Sort function Correct Sort
doc sort ip 1 doc sort ip 2

Sort IP address by formula

Sort IP address by VBA

Sort IP address by Text to Columns


Sort IP address by formula


Use a formula to fill the IP address then sort.

1. Select a cell adjacent to the IP address and type this formula

=TEXT(LEFT(A1,FIND(".",A1,1)-1),"000") & "." & TEXT(MID(A1,FIND( ".",A1,1)+1,FIND(".",A1,FIND(".",A1,1)+1)-FIND(".",A1,1)-1),"000") & "." & TEXT(MID(A1,FIND(".",A1,FIND(".",A1,1)+1)+1,FIND(".",A1, FIND(".",A1,FIND(".",A1,1)+1)+1)-FIND(".",A1,FIND(".",A1,1)+1)-1), "000") & "." & TEXT(RIGHT(A1,LEN(A1)-FIND(".",A1,FIND(".",A1,FIND( ".",A1,1)+1)+1)),"000")

press Enter key, and drag fill handle over cells to apply this formula.
doc sort ip 3

2. Copy the formula results and paste them as value in the next column. See screenshot:

doc sort ip 4
doc sort ip 5

3. keep the pasted value selected, and click Data > Sort A to Z.
doc sort ip 6

4. In the Sort Waring dialog, keep Expand the selection checked.
doc sort ip 7

5. click Sort. Now the IP addresses have been sorted from low to high.
doc sort ip 2

You can remove the helper columns.


Sort IP address by VBA

Here is a VBA code that also can help you.

1. Press Alt + F11 keys to enable the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, copy and paste the code to the blank script.

VBA: Fill IP address

Sub FormatIP()
'UpdatbyExtendoffice20171215
    Dim xReg As New RegExp
    Dim xMatches As MatchCollection
    Dim xMatch As Match
    Dim xRg As Range
    Dim xCell As Range
    Dim I As Long
    Dim xArr() As String
    On Error Resume Next
    Set xRg = Application.InputBox("Select cells:", "KuTools For Excel", Selection.Address, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    With xReg
        .Global = True
        .Pattern = "\d{1,3}\.+\d{1,3}\.+\d{1,3}\.+\d{1,3}"
        For Each xCell In xRg
            Set xMatches = .Execute(xCell.Value)
            If xMatches.Count = 0 Then GoTo xBreak
            For Each xMatch In xMatches
                xArr = Split(xMatch, ".")
                For I = 0 To UBound(xArr)
                    xArr(I) = Right("000" & xArr(I), 3)
                    If I <> UBound(xArr) Then
                        xArr(I) = xArr(I) & "."
                    End If
                Next
            Next
            xCell.Value = Join(xArr, "")
xBreak:
        Next
    End With
End Sub

doc sort ip 8

3. Then click Tools > Reference, and check Microsoft VBScript Regular Expressions 5.5 in the popping dialog.

doc sort ip 9
doc sort ip 10

4. Click OK and press F5 key, a dialog pops out to remind you to select a range to work.
doc sort ip 11

5. Click OK. Then the IP addresses have been filled with zero.

6. Select the IP addresses and click Data > Sort A to Z to sort them.


Sort IP address by Text to Columns

Actually, the Text to Columns feature can do you a favor in Excel too.

1. Select the cells you use, and click Data > Text to Columns. See screenshot:
doc sort ip 12

2. In the Convert Text to Columns Wizard dialog, do as below:

Check Delimited, and click Next;

Check Other and type . into the textbox, and click Next;

Select a cell next to the IP address to place the result. Click Finish.

doc sort ip 13
doc sort ip 14
doc sort ip 15

3. The select all cells containing the IP addresses and the split cells, and click Data > Sort.
doc sort ip 16

4. In the Sort dialog, clicking Add level to sort data from column B to E (the split cells). See screenshot:
doc sort ip 17

5. Click OK. Now the columns have been sorted.
doc sort ip 18


Advanced Sort

-- By Last Name (sort data based on criteria, such as sort by text length, sort by last name. sort by absolute value and so on.)
doc advanced sort last name


Recommended Productivity Tools

Office Tab

gold star1 Bring handy tabs to Excel and other Office software, just like Chrome, Firefox and new Internet Explorer.

Kutools for Excel

gold star1 Amazing! Increase your productivity in 5 minutes. Don't need any special skills, save two hours every day!

gold star1 300 New Features for Excel, Make Excel Much Easy and Powerful:

  • Merge Cell/Rows/Columns without Losing Data.
  • Combine and Consolidate Multiple Sheets and Workbooks.
  • Compare Ranges, Copy Multiple Ranges, Convert Text to Date, Unit and Currency Conversion.
  • Count by Colors, Paging Subtotals, Advanced Sort and Super Filter,
  • More Select/Insert/Delete/Text/Format/Link/Comment/Workbooks/Worksheets Tools...

Screen shot of Kutools for Excel

Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    bobulus · 3 months ago
    la formule traduite en Francais :
    =TEXTE(GAUCHE(I6;TROUVE(".";I6;1)-1);"000") & "." & TEXTE(STXT(I6;TROUVE( ".";I6;1)+1;TROUVE(".";I6;TROUVE(".";I6;1)+1)-TROUVE(".";I6;1)-1);"000") & "." & TEXTE(STXT(I6;TROUVE(".";I6;TROUVE(".";I6;1)+1)+1;TROUVE(".";I6; TROUVE(".";I6;TROUVE(".";I6;1)+1)+1)-TROUVE(".";I6;TROUVE(".";I6;1)+1)-1); "000") & "." & TEXTE(DROITE(I6;NBCAR(I6)-TROUVE(".";I6;TROUVE(".";I6;TROUVE( ".";I6;1)+1)+1));"000")
  • To post as a guest, your comment is unpublished.
    Chris · 10 months ago
    Why does the formula not work for the last octet? It adds zeros to the 3rd octet but not the last? So frustrating.
    • To post as a guest, your comment is unpublished.
      Sunny · 7 months ago
      I have tested the formula before I post it, it can work for the last octer. Have you checked the formula you pasted is correct?