January 15, 2013|30 comments
**Download this tutorial (zipped pdf)**
A question popped up on my Twitter feed this week - how to sort a list of IP addresses in Excel. At first glance, this should be simple. However, because Excel treats the IP addresses as strings and not numbers, the sort order is not correct, for example172.24.161.200will come sooner172.24.161.8(2 comes before 8 in alphabetical order). This tutorial shows you how to properly sort a list of IP addresses.
Scenario
For demonstration purposes, I have a small list of IP addresses. In reality, you would probably have a much longer list. This list shows the IP addresses used in a company and the location of the computer/device using them.
Sort the list - 1
Select the range A1:B6 and selectKindof theFactstab on the ribbon (or theFactsmenu depending on which version of Excel you are using)
Sort the list - 2
Be sure to check it"My data has headers"box (1)
Select to sort this data block by IP address"IP adres"from the drop-down list in the"Column"section 2)
Note that in the"Order"section (3) is shown as"All Z", which indicates that Excel treats the IP addresses as strings rather than numeric values (if it were treating them as numeric values, it would say "smallest to largest").
ClickOK(4)
Sort the list - 3
The list of IP addresses is indeed sorted, but not numerically
Split the IP address - 1
To properly sort a list of IP addresses, 3 steps are required. The first step is to split each IP address into individual cells. The screenshot below is the desired end result
Split the IP address - 2
Use Excel's instead of doing it manuallyText to Columnsfunction.
Insert 4 blank columns to the right of column A first, otherwise you will overwrite existing data when the Text to Columns command is issued.
Split the IP address - 3
Select all cells with the IP addresses (A2:A6)
SelectText to Columnsof theFactstab on the ribbon (or theFactsmenu depending on which version of Excel you are using)
Split the IP address - 4
The Text to Columns wizard starts. Ensure that"limited"is selected and clickFollowing
Split the IP address - 5
In step 2 of the wizard, check the option"Other"box and type onepunt(dot) in the adjacent box. This tells Excel that the dot character is the separator, i.e. the character used to split the string into 4 separate parts.)
ClickFollowing
Split the IP address - 6
At step 3 of the wizard, make sure that theDestinationfield contains=$A$2. This is where Excel stores the split text. Note that it is not necessary to select A2:D6. Excel uses as many columns as needed (that's why extra columns were inserted earlier).
ClickFinish
Split the IP address - 7
This is the result
Sort the list - 1
The second step of the 3-step process is to sort the list.
SelectA2:F6and use the settings according to the screenshot. Note that in the "Order" section, it shows as "Smallest to Largest", indicating that Excel treats the IP address elements as numerical values.
Sort the list - 2
This is the result of the multi-column sort:
Recreate the original IP addresses
The last step of the 3-step process is to combine the elements back into a single string.
Enter this formulaE2:
=SAMENVOEGEN(A2,"",B2,"",C2,"",D2)
The Concatenate function is used to create a single string from multiple cells and/or literal characters. In this case, the function concatenates the contents of A2, a period, the contents of B2, a period, the contents of C2, a period, and the contents of D2.
Copy the formula toE3:E6
Clean up - 1
Columns B:D can now be removed, however the formula in column E refers to these columns. If these columns are removed, the formulas will be broken. Therefore, before removing columns B:D:
Select cellsE2:E6
Select theTo copycommando
SelectA2
Click on the bottom part of thePlacingknob
SelectWorth places
The reason to use Paste Values instead of Paste is that Excel copies the result of the formula, rather than the formula itself.
Clean up - 2
Finally, remove columns B:D
**Download this tutorial (zipped pdf)**
** Updated April 10, 2011: I recently discovered an add-in that can sort a list of IP addresses with a few clicks. More informationhere
Sumeshon November 27, 2013 at 7:10 PM
You're great, it was a bit difficult because I'm not that proficient with EXCEL. But this worked with a minor glitch for me.
You rock.Mike Tomason November 27, 2013 at 10:31 PM
Thank you! I'm glad you found the tutorial helpful
André Robergeon December 3, 2013 at 2:31 pm
There is a goodExcel-spreadsheet-toolwhich has a function to convert IP addresses into decimal numbers. Once that's done, sorting is really easy.
andreon February 16, 2014 at 2:56 am
You can just have the text to the column output the results to column D:E
sort open (spend selection) and follow above
no need formula and all copy/paste then you can easily delete the D:E
andreon February 16, 2014 at 2:56 am
oops correct that D:G you just have to select where the first one goes and Excel will do the rest
Mike Tomason February 16, 2014 at 3:22 PM
Thank you or your comment Andrew. I'm not 100% sure what you mean though, as column F had some content - the location of each PC. You also need the CONCATENATE formula to put it all back together so that each IP address is stored in a single cell instead of being split into 4 cells.
Chrison April 28, 2014 at 9:20 PM
I'm not sure how pasting the =CONCATENATE(A2,"",B2,"",C2,"",D2) into other rows besides the 2nd row gives you the IP address for that corresponding row. I need a formula that I can paste into any cell in that column without editing that formula for that particular row. If I paste that "=CONCATENATE(A2,"","B2,"","C2,"","D2)" into every other row, I get the same output as the 2nd row since the formula only honors A2,B2 , C2 and D2. Is there a wildcard variable you can use instead of the 2 (for the 2nd row)?
Thank you,
Chris
Mike Tomason May 3, 2014 at 1:35 PM
Hi Chris. The CONCATENATE function in E2 concatenates the contents of the 4 cells on the left. When the function is copied to rows 3, 4, 5, and 6, Excel does not copy the function itself, but copies the "merge the values in the 4 cells on the left" statement, so the function in E3 becomes =CONCATENATE (A3,"",B3,"",C3,"",D3) and so on
-Mike-
Calvinon July 22, 2014 at 5:45 am
Thank you very much, the tutorial worked like a charm.
Aaronon December 17, 2014 at 7:29 PM
Hello Chris,
I know this is a day late and a dollar short, but just right and copy E2 and drag the cursor to the bottom of the column. Then select Ctrl-D on the keyboard and the rows will automatically update with the correct cell numbers.Nick Morganon February 26, 2015 at 12:48 p.m
An alternative method could be to use 'find a replacement' on a column of IP addresses to remove the decimals, e.g. find all 10.0.5. and replace with 1005. A sort can then be performed on the column as it is in standard numeric format without the dots (e.g. 10051, 10052 etc.) Once the column is sorted, another find and replace can be performed to return to revert to IP format, e.g. find all 1005 and replace them with 10.0.5.
Tomon March 4, 2015 at 7:55 am
Thanks for the good and accurate tutorial!
akshayon March 10, 2015 at 5:14 am
Hi Mike, that was a really nice trick to sort IP addresses. Splitting the data and applying sorting and recombining. Thank you
LeeMon April 3, 2015 at 9:32 PM
Since mine were all in the same subnet 255.255.255.0 I added a column for LEN (ipaddress) sorted by that and then by the IP.
kimon April 20, 2015 at 9:58 am
For all Excel users dealing with the IP address, one is rightExcel add-inthat detects IP address by country, region, city, latitude, longitude, zip code, ISP, domain name, time zone, connection speed, IDD code, area code, weather station code, weather station name, MCC, MNC, mobile brand name, altitude, and usage type.
Maton June 5, 2015 at 6:33 PM
Another easy option is to just use CTRL F to open the Find menu, click the Replace tab, and then replace all periods (.) with hyphens (-). Then select all data and sort in ascending order. After they are OK, you can use VLOOKUP on them. After you're done with everything, just replace all the hyphens (-) with periods (.) again and they'll be fine and in the correct format.
JimiDDon June 24, 2015 at 12:59 pm
An excellent way to deal with difficult IP addresses.
Thank you.Ledskofon August 8, 2015 at 4:07 am
This doesn't work for the same reason that having dots doesn't work.
stricton September 18, 2015 at 8:24 am
Well done! This seems to be the easiest and simplest way possible...
Twoon October 22, 2015 at 5:51 pm
Adding the zeros to the IP addresses sorts them correctly
e.g.: 192.168.1.1 retyped as 192.168.11.001 as long as they are in the same subnetthaton November 6, 2015 at 7:25 am
thanks for a great idea.
BBNon March 25, 2016 at 4:23 pm
YOU MADE MY DAY. THANK YOU MAN…..
Kamal Prasadon May 12, 2016 at 9:39 PM
Hoi,
I need your help to create an excel formula for creating an IP database.
I have a device IP address (say 192.168.0.49), need to add another IP (next hop: device IP address +1, i.e. 192.168.0.50).
another example... Device IP: 192.168.0.3, the next hop is 192.168.0.4
need to create the list with more than 100 IP addresses of devices, so I think Excel would be an easy way.
Thank youMike Tomason May 12, 2016 at 9:54 PM
If it's as simple as 192.168.0.49, 192.168.0.50, 192.168.0.51...
Then type 192.168.0.49 in a cell, point your mouse to the bottom right corner of the cell and drag down. But I guess it's more complicated because you can't go beyond 255 for the 4th octet. Let me know
old manon July 21, 2016 at 12:12 PM
best answer
Ray O.on July 27, 2016 at 10:39 PM
Instead of using "=concatenate(A2,B2,C2,D2)", I find it easier to just type "=A2&B2&C2&D2". Save some keystrokes...
Gerard Royon September 22, 2016 at 8:48 PM
Merge doesn't always work, maybe based on cell formatting? A better way to concatenate the cells back into one string could be as follows: &”.”&
=D1&”.”&E1&”.”&F1&”.”&G1Linixon January 19, 2017 at 9:40 PM
Keep the original column of data. Create 4 new columns to the right of the IP address (B, C, D, E) . Copy the IP addresses (column A) and paste them in column B. Now continue with the text to table using column B.
Yothinsumphun, Yuttanaon March 4, 2017 at 12:24 PM
I used the DEC2BIN function.
DEC2BIN(number, [places])
First, use the FIND function to find "." and separate 4 octets.
1e Octet: LEFT(A1,FIND(“.”,A1)-1)
2nd Octet: RIGHT(LEFT(A1,FIND(“.”,A1,FIND(“.”,A1)+1)-1),FIND(“.”,A1))
3rd Octet: RIGHT(LEFT(A1,FIND(“.”,A1,FIND(“.”,A1,FIND(“.”,A1)+1)+1)-1),FIND(“.”,A1 ,FIND(“.”,A1,FIND(“.”,A1)+1)+1)-FIND(“.”,A1,FIND(“.”,A1)+1)-1)
4e Octet : RECHTS(A1,4-FIND(“.”,RIGHT(A1,4)))+0
then combine DEC2BIN function in each octet and put in 8 bit.
After all, the formula is
=DEC2BIN(LEFT(A1,FIND(“.”,A1)-1),8)&”.”&DEC2BIN(RIGHT(LEFT(A1,FIND(“.”,A1,FIND(“.”,A1)+ 1)-1),FIND(“.”,A1)),8)&”.”&DEC2BIN(RIGHT(LEFT(A1,FIND(“.”,A1,FIND(“.”,A1,FIND(“. ”,A1)+1)+1)-1),FIND(“.”,A1,FIND(“.”,A1,FIND(“.”,A1)+1)+1)-FIND(“.” ,A1,FIND(“.”,A1)+1)-1),8)&”.”&DEC2BIN((RIGHT(A1,4-FIND(“.”,RIGHT(A1,4)))+0) .8)
sort with bit column A->Z.
SEAK, Teng-Fongon January 6, 2018 at 9:51 am
@Yothinsumphun, Yuttana
Your formulas to find 2nd and 4th octets are unfortunately wrong. You could use 111.22.3.4 to see what I mean.It should have been for the 2nd octet
RIGHT(LEFT(A1,FIND(“.”,A1,FIND(“.”,A1)+1)-1), LEN(LEFT(A1,FIND(“.”,A1,FIND(“.”,A1 )+1)-1))-FIND(“.”,A1))
But a shorter one would be
LEFT(RIGHT(A1,LEN(A1)-FIND(“.”,A1)), SEARCH(“.”,RIGHT(A1,LEN(A1)-FIND(“.”,A1)))-1)The 4th octet should have been
RIGHT(A1,LEN(A1)-FIND(“.”,A1,FIND(“.”,A1,FIND(“.”,A1)+1)+1))Instead of using LEFT(), RIGHT() and embedded FIND() I would have used the SUBSTITUTE() trick to find the positions of the points.
On the other hand, I would have displayed zero leading decimal values instead of binary strings of values. For example, the first octet would have been
TEXT(LEFT(A1,LOOKUP(“.”,A1)-1), “000”)This way we can still sort the column and the strings displayed are easy to read and errors easy to spot.