Sorting IP Addresses

by Allen Wyatt
(last updated October 15, 2016)


Chuck has a worksheet that, in one column, contains a series of IP addresses. These are in the familiar format of If he sorts the addresses, they are not numerically sorted. For instance, Excel places between and Chuck wonders if there is a way to sort a column of IP addresses so they appear in the proper sequence.

This happens because Excel views an IP address as text, not as a number or a series of numbers. There are a few ways you can work around the problem, a few of which I'll discuss in this tip. You should choose the approach that is right for your needs, as defined by your data and how you use that data.

One approach is to make sure that each octet of your IP addresses consist of three digits. (An octet is each part of the IP address, separated by periods.) For instance, instead of an address such as, you would use This "front pads" each octet with zeros and, if all of your IP addresses are in this format, they will sort correctly.

If you prefer to use a formula to ensure the front-padding of each octet, you could use the following:

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

This formula is quite long, but it is still a single formula. Place it in the column next to your first IP addresss (assuming that address is in cell A1) and then copy it down as many rows as required. When you do your sorting, sort by column B, and the addresses will be in the proper sequence.

If you work with a lot of IP addresses, you may want to create a user-defined function that will front-pad each octet of the IP address with zeros and then return a fully formatted IP. The following will perform the task:

Function FormatIP(IPAddr As String) As String
    Dim Dot1 As Integer
    Dim Dot2 As Integer
    Dim Dot3 As Integer
    Dim Octet1 As String
    Dim Octet2 As String
    Dim Octet3 As String
    Dim Octet4 As String

    Dot1 = InStr(1, IPAddr, ".", vbTextCompare)
    Dot2 = InStr(Dot1 + 1, IPAddr, ".", vbTextCompare)
    Dot3 = InStr(Dot2 + 1, IPAddr, ".", vbTextCompare)

    Octet1 = Left(IPAddr, Dot1-1)
    Octet2 = Mid(IPAddr, Dot1+1, Dot2-Dot1-1)
    Octet3 = Mid(IPAddr, Dot2+1, Dot3-Dot2-1)
    Octet4 = Mid(IPAddr, Dot3+1, Len(IPAddr))

    FormatIP = Right("000" & Octet1, 3) & "."
    FormatIP = FormatIP & Right("000" & Octet2, 3) & "."
    FormatIP = FormatIP & Right("000" & Octet3, 3) & "."
    FormatIP = FormatIP & Right("000" & Octet4, 3)
End Function

In Excel, then, you could use the UDF in this manner, assuming your original IP address in in cell A1:


Another approach is to simply divide the IP addresses into separate columns, putting each octet in its own column. This is easy to do if you use the Text to Columns tool, in this manner:

  1. Make sure there are three blank columns to the right of your IP addresses.
  2. Select the range of cells containing the IP addresses.
  3. Display the Data tab of the ribbon.
  4. Click the Text to Columns tool, in the Data Tools group. Excel starts the Convert Text to Columns Wizard. (See Figure 1.)
  5. Figure 1. The beginning of the Convert Text to Columns Wizard.

  6. Choose the Delimited radio button.
  7. Click on Next.
  8. Select the checkbox next to Other and, to the right of the check box, enter a period.
  9. Click on Finish.

Once done, you can sort the four columns as you would normally sort numbers. Then, when you want to put the IP addresses back together, you could use a formula such as this:

=A1 & "." & B1 & "." & C1 & "." & D1

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13481) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...


Copying and Pasting Non-Contiguous Ranges of Cells

Excel makes it easy to copy and paste a range of cells. Easy, that is, unless the range isn't contiguous. If you have a ...

Discover More

Shading Table Rows

Need to format the rows of a table so that your data is showcased better? Here are a few ways you can get the shading you ...

Discover More

Selecting Default Paragraph Formatting

Want to return a paragraph's formatting back to it's pristine, unaltered state? You can do so by using the shortcut ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Sorting ZIP Codes

Sorting ZIP Codes can be painless, provided all the codes are formatted the same. Here's how to do the sorting if you ...

Discover More

Sorting a Range of Cells

When you sort data in a worksheet, you don't need to sort everything at once. You can sort just a portion of your data by ...

Discover More

Too Many Formats when Sorting

Sorting is one of the basic operations done in a worksheet. If your sorting won't work and you instead get an error ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 1?

2017-07-10 18:42:36


Here's my solution (uses Excel and Notepad++):

Paste your list of IP addresses on line 2 in any column.
Select the column.
Go to the Data tab.
Click Text to Columns.
Click Delimited.
Click Next.
Check "Others" and enter a period.
Click Finish.

Now your IP addresses will be split across four columns.
Highlight those four columns.
Go to the Home tab.
Click Sort & Filter.
Click Filter.
Now line 1 will have a drop-down sort menu for each column.
Start at the 4th column, click the drop-down menu. Sort Smallest to Largest
Repeat for the 3rd column, then the 2nd, then the first.

All of your IP addresses are now sorted.
Highlight all of the IP addresses and copy.
Paste into Notepad++
Double click on the space between two numbers (where a period should go)
Press CTRL F
Click the Replace tab
"Find what" should be filled with the space you double clicked.
Click "Replace with" and type in a period.
Click Replace All.

Your sorted IP addresses are now recombined.

In Excel, you can disable Text to Columns by highlighting a column, Clicking Text to Columns in the Data tab, click Next with Delimiter marked, uncheck all delimiter boxes and pressing Finish.

Now you can safely paste the sorted IP addresses back into Excel.

Once you get the hang of it you can sort any number of IPs in about 30 seconds.
This could be faster with a way to recombine the IPs within Excel, but I don't know how.

2017-03-28 09:58:21

MIchael Armstrong

What was @debbie's solution? I often see references to comments that aren't there. Wazzup with that?

2017-03-27 16:47:20

Justin Grote

As a minor update, if you use excel tables, you can just reference a column name. The below formula will work as long as your IP address column is named "IPv4" but of course you can update it however you want.

Easier than having to edit it every time if your IP addresses aren't in the "A" column


2016-10-17 08:51:45


my solution was the easiest of all the comments you have there.

2016-10-17 06:18:01

Willy Vanhaelen


I tried but it crashes because I always use the Option Explicit statement. So I had to add an 8th line "Dim P As Integer" -:)

But you can even go for 6 lines if you delete "Dim Ar As Variant" but then you must also delete the Option Explicit statement which is not recommended and all you undeclared variables are then Variants.

2016-10-16 10:10:42

Michael (Micky) Avidan

@Willy Vanhaele,
Try "tiny" 7 Lines:
Function FormatIP(IPAddr As String) As String
Dim Ar As Variant
Ar = Split(IPAddr, ".")
For P = 0 To 3
Ar(P) = Right(Ar(P) + 1000, 3)
FormatIP = Join(Ar, ".")
End Function
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2017)

2016-10-15 05:51:42

Willy Vanhaelen

The 20 lines macro of this tip can be reduced to this tiny 8 lines one:

Function FormatIP(IPAddr As String) As String
Dim Ar As Variant
Ar = Split(IPAddr, ".")
Ar(0) = Right(Ar(0) + 1000, 3)
Ar(1) = Right(Ar(1) + 1000, 3)
Ar(2) = Right(Ar(2) + 1000, 3)
Ar(3) = Right(Ar(3) + 1000, 3)
FormatIP = Join(Ar, ".")
End Function

Simplicity is the ultimate sofistication (Leonardo du Vinci).

This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.