Solved

Excel: Get third octet of an IP address

Posted on 2010-11-24
11
3,289 Views
Last Modified: 2012-05-10
Hello everybody

Can you guys help me find a way to get the third octet from an IP address?
What i basically need is everything that is in between (but not including) the second and third dot (.).

Examples:

192.168.12.77
172.16.215.33
10.0.1.1

I should get the following:
12
215
1

Thank you :)
0
Comment
Question by:Advizor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34204306
Would this help?
Sub Sample()
    Dim strIP As String, MyArray() As String
    
    strIP = "192.168.12.77"
    
    MyArray = Split(strIP, ".")
    
    MsgBox MyArray(2)
    
End Sub

Open in new window

0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 34204350
Here's a formula suggestion:

=LEFT(MID(A1,FIND(".",A1,FIND(".",A1)+1)+1,99),FIND(".",MID(A1,FIND(".",A1,FIND(".",A1)+1)+1,99))-1)

start in row 1 and copy down

cheers, teylyn
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 34204403
Building on SiddhartRout's VBA suggestion: if you want a dynamic VBA solution, you could try this user defined formula

Function Sample(strIP As String)
    Dim MyArray() As String
    MyArray = Split(strIP, ".")
    Sample = MyArray(2)
End Function

Open in new window


Copy and paste the code into a regular code module and then use

=sample(A1)

to calculate the result.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34204425
=TRUNC("0"&MID(A1,FIND("|",SUBSTITUTE(A1&"..",".","|",2))+1,10))

Won't get any #value #n/a errors if it is not a valid ip address, 0 instead
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34204451
Nevermind, please ignore the previous comment. Been working at it too long - there are better answers.
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 34204475
CK, nice one with the TRUNC. It does work for my data sample, so no need to apologize.
0
 
LVL 50

Expert Comment

by:barry houdini
ID: 34204646
Try

=TRIM(LEFT(RIGHT(SUBSTITUTE(A1,".",REPT(" ",9)),18),9))

regards, barry
0
 
LVL 50

Expert Comment

by:barry houdini
ID: 34204854
...or....

=INT(-LOOKUP(0,-RIGHT(A1,{1,2,3,4,5,6,7})))

barry
0
 

Author Comment

by:Advizor
ID: 34204871
Thank you all for your suggestions.

I've managed to get something working from cyberkiwi's formula.
I ran into issues regarding international/regional settings when trying to use an unmodified version of cyberkiwi's.
Truncate expects a number, but on my danish version of Excel it does not see the preliminary result (on which to truncate) as a number. Yay! :/

Here is what I got to work:

=MID(B9;FIND("|";SUBSTITUTE(B9 & "..";".";"|";2))+1;FIND("|";SUBSTITUTE(B9 & "..";".";"|";3))-FIND("|";SUBSTITUTE(B9 & "..";".";"|";2))-1)

Thank you cyberkiwi (and all others as well) :)
0
 

Author Closing Comment

by:Advizor
ID: 34204914
Had to modify the suggestion due to regional differences between the two computers.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34207675
Though I am happy for the points, I thought the other formulas worked as well.
Barry has a knack for formulaes, and his two options are both amazing pieces of art.

While the 2nd one returns the wrong result when the fourth octet is 0, I think the 1st one works.

Regards
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question