## 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

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.

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

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

4. In the Sort Waring dialog, keep Expand the selection checked.

5. click Sort. Now the IP addresses have been sorted from low to high.

You can remove the helper columns.

#### Sort IP address by VBA

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.

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

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

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

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:

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.

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

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

5. Click OK. Now the columns have been sorted.

### Best Office Productivity Tools

 ðŸ¤– Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functionsâ€¦ Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ... Super Lookup: Multiple Criteria VLookup  |   Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup .... Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List .... Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ... Featured Features: Grid Focus   |  Design View   |   Big Formula Bar   |  Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells   |  Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ... Top 15 Toolsets:  12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

#### Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

• Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
• Open and create multiple documents in new tabs of the same window, rather than in new windows.
• Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
=TEXTO(IZQUIERDA(A1,ENCONTRAR(".",A1,1)-1),"000") & "." & TEXTO(EXTRAE(A1,ENCONTRAR( ".",A1,1)+1,ENCONTRAR(".",A1,ENCONTRAR(".",A1,1)+1)-ENCONTRAR(".",A1,1)-1),"000") & "." & TEXTO(EXTRAE(A1,ENCONTRAR(".",A1,ENCONTRAR(".",A1,1)+1)+1,ENCONTRAR(".",A1, ENCONTRAR(".",A1,ENCONTRAR(".",A1,1)+1)+1)-ENCONTRAR(".",A1,ENCONTRAR(".",A1,1)+1)-1), "000") & "." & TEXTO(DERECHA(A1,LARGO(A1)-ENCONTRAR(".",A1,ENCONTRAR(".",A1,ENCONTRAR( ".",A1,1)+1)+1)),"000")
This comment was minimized by the moderator on the site
Thanks for your help on translating the formula.
This comment was minimized by the moderator on the site
This is great! Very much appreciated. It would be nice to add to the VB code to reverse (put back) the IP's in the original format (without the extra leading 0's) after having sorted them. :) I would be very interested in that where you can run this script, sort results, then revert back to original format.
This comment was minimized by the moderator on the site
thank you, very useful !
This comment was minimized by the moderator on the site
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")
This comment was minimized by the moderator on the site