+ Reply to Thread
Results 1 to 7 of 7

sorting IP/subnet

  1. #1
    Registered User
    Join Date
    02-06-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    sorting IP/subnet

    Hi
    I am trying to sort IP's in the following format
    10.0.0.0/8
    10.0.1.0/16
    10.0.0.0/16
    10.0.4.0/16
    10.0.4.64/27
    10.0.4.0/30
    I got a formula which works well when the subnet mask i.e. /xx is not appended to the IP

    =RIGHT("00"&LEFT(A2,FIND(".",A2)-1),3)&"."&RIGHT("00"&MID(A2,FIND(".",A2)+1,FIND("|",SUBSTITUTE(A2,".","|",2))-FIND(".",A2)-1),3)&"."&RIGHT("00"&MID(A2,FIND("|",SUBSTITUTE(A2,".","|",2))+1,FIND("|",SUBSTITUTE(A2&".",".","|",3))-FIND("|",SUBSTITUTE(A2,".","|",2))-1),3)&"."&RIGHT("00"&RIGHT(A2,LEN(A2)-FIND("|",SUBSTITUTE(A2,".","|",LEN(A2)-LEN(SUBSTITUTE(A2,".",""))))),3)

    BUT I need the subnet mask and the expected result for the above example will be
    10.0.0.0/8
    10.0.0.0/16
    10.0.1.0/16
    10.0.4.0/16
    10.0.4.0/30
    10.0.4.64/27

    Any help is appreciated
    Thanks

  2. #2
    Forum Expert newdoverman's Avatar
    Join Date
    02-07-2013
    Location
    Port Dover, Ontario, Canada
    MS-Off Ver
    2010
    Posts
    10,330

    Re: sorting IP/subnet

    With the data in column A, use Data, Text to columns with the / as the delimiter. This will split the sub-net into the column B. Then, select both columns and first select to sort by column A and add a level and sort by column B. Both should be sort from A-Z or smallest to largest (which ever you are presented with)

    This is the result:

    A
    B
    1
    10.0.0.0
    8
    2
    10.0.0.0
    16
    3
    10.0.1.0
    16
    4
    10.0.4.0
    16
    5
    10.0.4.0
    30
    6
    10.0.4.64
    27
    <---------If you like someone's answer, click the star to the left of one of their posts to give them a reputation point for that answer.
    Ron W

  3. #3
    Registered User
    Join Date
    02-06-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: sorting IP/subnet

    I am not sure how to add another level of sort in column B as you suggest since when I do that the sorting in column A gets messed up.

  4. #4
    Forum Expert newdoverman's Avatar
    Join Date
    02-07-2013
    Location
    Port Dover, Ontario, Canada
    MS-Off Ver
    2010
    Posts
    10,330

    Re: sorting IP/subnet

    This is step by step. The data is in column A.
    1./ Split the sub-net into column B
    Select all of the addresses
    Click on the Data Tab, Text to Columns, Delimited, Other and use / as the delimiter.
    Click Finish.

    All Sub-nets should now be in column B

    2./ Select all the data in columns A and B
    3./ Click on the Data tab, and click on the sort button.
    4./ A dialogue will open. If you don't have headers uncheck the box for My Data Has Headers.
    5./ In the Sort By Select Column A, Values, A-Z
    6./ Click on Add Level (top left just above the word Column.
    7./ A new row is added to the dialogue....select sort by Column B, Values, Smallest to Largest
    8./ Click OK.

    Your data will be sorted exactly as I have shown in my previous post.

  5. #5
    Registered User
    Join Date
    02-06-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: sorting IP/subnet

    Since IP addresses can have values like 10.0.1.20/12 , 10.0.128.0/15 or 10.0.2.0/16 it becomes important to first split subnet in column B , apply the formula
    =RIGHT("00"&LEFT(A2,FIND(".",A2)-1),3)&"."&RIGHT("00"&MID(A2,FIND(".",A2)+1,FIND("|",SUBSTITUTE(A2,".","|",2))-FIND(".",A2)-1),3)&"."&RIGHT("00"&MID(A2,FIND("|",SUBSTITUTE(A2,".","|",2))+1,FIND("|",SUBSTITUTE(A2&".",".","|",3))-FIND("|",SUBSTITUTE(A2,".","|",2))-1),3)&"."&RIGHT("00"&RIGHT(A2,LEN(A2)-FIND("|",SUBSTITUTE(A2,".","|",LEN(A2)-LEN(SUBSTITUTE(A2,".",""))))),3)
    on only the IP that is column A and then use the new column and subnet with the 2 levels of sort.

    Thanks for your help ...both techniques combined solved my problem

  6. #6
    Forum Expert newdoverman's Avatar
    Join Date
    02-07-2013
    Location
    Port Dover, Ontario, Canada
    MS-Off Ver
    2010
    Posts
    10,330

    Re: sorting IP/subnet

    Alternate formulae to split the IP from the subnet mask.

    To split the IP into column B enter this in B1 and copy down:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    To get the subnet into column C enter this in C1 and copy down.

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  7. #7
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373

    Re: sorting IP/subnet

    Hi

    newdoverman's solution is clear, efficient and easy to implement.

    If, however, for some reason you'd want no auxiliary columns and a dynamic solution (that would auto-update if you change the input IP's) you could use in B1:

    =INDEX($A$1:$A$6,MATCH(SMALL(MMULT((MID(SUBSTITUTE(SUBSTITUTE($A$1:$A$6,"/","."),".",REPT(" ",15)),1+{0,1,2,3,4}*15,15))*256^{4,3,2,1,0},{1;1;1;1;1}),ROWS($B$1:B1)),MMULT((MID(SUBSTITUTE(SUBSTITUTE($A$1:$A$6,"/","."),".",REPT(" ",15)),1+{0,1,2,3,4}*15,15))*256^{4,3,2,1,0},{1;1;1;1;1}),0))

    This in an array formula, you have to confirm it with CTRL+SHIFT+ENTER.

    Copy down

    Remark: this assumes the source IP's in A1:A6 with valid IP's in the format posted. It could be upgraded to allow for eventual empty cells inside, or other requirements.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help - find subnet corresponding to ip
    By silentQ in forum Excel General
    Replies: 1
    Last Post: 07-13-2013, 02:33 PM
  2. Need to take an IP Subnet Range and Output Subnet Mask
    By Markh7999 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-12-2013, 08:53 PM
  3. IP Subnet Calculations
    By MoonWeazel in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-22-2010, 10:10 AM
  4. IP Subnet Finder
    By samtoucan in forum Excel General
    Replies: 2
    Last Post: 08-31-2010, 07:49 AM
  5. IP/Subnet Filter help
    By ca_engineer in forum Excel General
    Replies: 1
    Last Post: 10-15-2009, 08:47 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1