How do I get cells in Excel that contain IP addresses to sort properly?

As you may have realised, your IP addresses are treated as text and not numbers. They are being sorted as text, which means that addresses beginning with "162" will come before addresses beginning with "20." (because the character "1" comes before the character "2".

You can use the formula provided in this answer: https://stackoverflow.com/a/31615838/4424957 to split the IP address into its parts.

If your IP addresses are in columns A, add columns B-E as shown below.

enter image description here

Enter the formula

=VALUE(TRIM(MID(SUBSTITUTE($A2,".",REPT(" ",999)),(B$1)*999-998,999)))

in cell B2 and copy it to columns B-E in all rows to get the four parts of each IP address. Now sort the whole range by columns B through E (in that order) as shown below:

enter image description here

If you don't want to see the helper columns (B-E), you can hide them.


The most easiest, 3 steps Solution I can suggest you are,,,

  1. Select the IP Address Column, apply Text to Column command.

  2. In adjacent Column write this Formula

    =CONCATENATE(B3,".",C3,".",D3,".",E3)

  3. Finally Sort in Ascending order.

Check the Screen Shot.

enter image description here

NB:

Red is Original IP Address (in Column A).

Green after applied Text to Column (Column B to E).

Black is After applied Concatenate & Sorting (Column F).

The reason is very simple originally IP address is Text Data and Excel doesn't accepts any Cell Format to turn it to Number.

Hope this help you.


Here'a a VBA function I wrote some time ago to solve the same problem. It generates a padded version of an IPv4 address which sorts correctly.

Function SortAddress(Address As String)                     '   format address as XXX.XXX.XXX.XXX to permit sorting

Dim FirstByte As Integer, LastByte As Integer, I As Integer

SortAddress = ""
FirstByte = 1

For I = 0 To 2                                          '   process the first three bytes

    LastByte = InStr(FirstByte, Address, ".")           '   find the dot
                                                        '   append the byte as 3 digits followed by dot
    SortAddress = SortAddress & Format(Mid(Address, FirstByte, LastByte - FirstByte), "000\.")

    FirstByte = LastByte + 1                            '   shift the start pointer

Next I

SortAddress = SortAddress & Format(Mid(Address, FirstByte), "000") ' process the last byte

End Function

Simple example:

Result

Result

Formulas

Formulas

You can sort by the 'Sortable' column and hide it.


Here is an answer that will take only 1 column of your table and converts the IPv4 address to base 10 numbering.

Since you are putting your data in column "M", this starts in cell M2 (M1 being the label). Encapsulating it as code gives one terrible mess, so I have used blockquote:

=INT(LEFT(M2, FIND(".", M2) - 1)) * 256 ^ 3 + INT(MID(M2, FIND(".", M2) + 1, FIND(".", M2, FIND(".", M2) + 1) - FIND(".", M2)-1)) * 256 ^ 2 + INT(MID(M2, FIND(".", M2, FIND(".", M2) + 1) + 1, FIND(".", M2, FIND(".", M2, FIND(".", M2) + 1) + 1) - FIND(".", M2, FIND(".", M2) + 1) - 1)) * 256 + INT(RIGHT(M2, LEN(M2) - FIND(".", M2, FIND(".", M2, FIND(".", M2) + 1) + 1)))

Not exactly the most easily readable formula, but you can just copy and paste into your cell (preferably N2 or something else in the same row as your first IP address). It presumes proper formatting of the IP address as error correction in the formula would make it even worse for human parsing.


If you don't want to use formulas or VBA, use Power Query. (In Excel 2016, Get & Transform, in Excel 2010 or 2013 install PowerQuery add-in to follow along).

  1. Bring the table into the PowerQuery editor.
  2. Duplicate the column by right clicking "Duplicate Column"
  3. "Split Column" by delimiter, on the Home tab. Select "Each occurrence of the delimiter"
  4. Sort each column Asc. from left to right.
  5. Select the previously split columns, right click and remove, close and load.