VBA: Parsing multiple IP addresses from a string

sushinutnc

New Member
Joined
Jul 23, 2009
Messages
15
I have some code that will parse the FIRST IP address it finds in a string. Could someone help with the code for continuing through the remaining part of the string and parsing out any additional IP addresses it finds and concatenating them with a comma and space (", ")?

There are specific rules for validating a "proper" IP, but for the purposes of my work, I don't need to validate. I found the following code on a google group (thanks Jeff M). All it does is parse the numeric text surrounding the 3 periods that all valid IPv4 addresses contain. For my purposes, it works.

*******************************
Function FindIP(IP As String) As String
Dim cnt As Integer, NoOfDots As Integer
Dim i As Integer, c As Integer

For i = 1 To Len(IP)
If IsNumeric(Mid(IP, i, 1)) Or Mid(IP, i, 1) = "." Then
cnt = cnt + 1
Else
'More than 6 consecutive char's that are
'either numeric or dot (".")
If cnt > 6 Then
FindIP = Mid(IP, i - cnt, cnt)

'Count the number of dot's
NoOfDots = 0
For c = 1 To Len(FindIP)
If Mid(FindIP, c, 1) = "." Then
NoOfDots = NoOfDots + 1
End If
Next c

'Check no of dots (s/b 3)
If NoOfDots = 3 Then
Exit Function
Else
FindIP = ""
End If

End If
cnt = 0
End If
Next i
End Function
*******************************

So, what I need is for this code to loop through the rest of the input string and pull out any additional IPs (an array?).

Here are two examples (there may be 100s of chars in a input field. These are just examples).

************

EXAMPLE INPUT1:
We have a problem with 192.168.3.43 and also with 172.16.234.12. Also might have a problem with 10.168.4.1.

OUTPUT with above code is:
192.168.3.43

DESIRED OUTPUT:
192.168.3.43, 172.16.234.12, 10.168.4.1
************

************
EXAMPLE INPUT2:
There is a system at 192.168.23.142, but it's not communicating with 192.168.4.132. Please call me at 555.432.2534 to see if it's talking to 10.4.22.3. My IP is 172.16.4.12. Thanks!

OUTPUT with above code is:
192.168.23.142

DESIRED OUTPUT:
192.168.23.142, 192.168.4.132, 10.4.22.3, 172.16.4.12
************


(BTW, I didn't want to get technical with this, but if anyone wants to validate parsed IPs, they are comprised of four, 8-bit numbers (octets) separated by three "dots". For the purposes of this discussion valid IPs could range from "0.0.0.0" to "255.255.255.255"; i.e., each octet can range from 0-255).

THANK you in advance for any help!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi sushinutnc
Welcome to the board

Try:
Code:
Function ExtractIPs(s As String)
 
With CreateObject("VBScript.RegExp")
    .Pattern = ".*?(\d{1,3}(\.\d{1,3}){3})|.*"
    .Global = True
    ExtractIPs = Replace(Trim(.Replace(s, " $1")), " ", ", ")
End With
End Function

In B1:

=ExtractIPs(A1)
Copy down


<TABLE style="BORDER-BOTTOM-COLOR: #cccccc; BORDER-RIGHT-WIDTH: 2px; BORDER-TOP-COLOR: #cccccc; BORDER-COLLAPSE: collapse; FONT-FAMILY: Arial,Arial; BACKGROUND: #fff; BORDER-TOP-WIDTH: 2px; BORDER-BOTTOM-WIDTH: 2px; BORDER-RIGHT-COLOR: #cccccc; FONT-SIZE: 10pt; BORDER-LEFT-COLOR: #cccccc; BORDER-LEFT-WIDTH: 2px" border=1 cellPadding=1><TBODY><TR><TH style="BORDER-BOTTOM-COLOR: #888888; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; BACKGROUND: #9cf; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px"> </TH><TH style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: center; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; BACKGROUND: #9cf; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px">A</TH><TH style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: center; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; BACKGROUND: #9cf; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px">B</TH><TH style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: center; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; BACKGROUND: #9cf; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px" width=30>C</TH></TR><TR><TD style="BORDER-BOTTOM-COLOR: #000000; TEXT-ALIGN: center; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #000000; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BACKGROUND: #9cf; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #000000; BORDER-LEFT-COLOR: #000000; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em">1</TD><TD style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: left; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em">We have a problem with 192.168.3.43 and also with 172.16.234
.12. Also might have a problem with 10.168.4.1.</TD><TD style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: left; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em">192.168.3.43, 172.16.234.12, 10.168.4.1</TD><TD style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: right; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em"> </TD></TR><TR><TD style="BORDER-BOTTOM-COLOR: #000000; TEXT-ALIGN: center; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #000000; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BACKGROUND: #9cf; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #000000; BORDER-LEFT-COLOR: #000000; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em">2</TD><TD style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: left; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em">There is a system at 192.168.23.142, but it's not communicat
ing with 192.168.4.132. Please call me at 555.432.2534 to se
e if it's talking to 10.4.22.3. My IP is 172.16.4.12. Thanks
!</TD><TD style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: left; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em">192.168.23.142, 192.168.4.132, 10.4.22.3, 172.16.4.12</TD><TD style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: right; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em"> </TD></TR><TR><TD style="BORDER-BOTTOM-COLOR: #000000; TEXT-ALIGN: center; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #000000; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BACKGROUND: #9cf; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #000000; BORDER-LEFT-COLOR: #000000; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em">3</TD><TD style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: right; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em"> </TD><TD style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: right; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em"> </TD><TD style="BORDER-BOTTOM-COLOR: #888888; TEXT-ALIGN: right; PADDING-BOTTOM: 0.25em; BORDER-RIGHT-WIDTH: 1px; BORDER-TOP-COLOR: #888888; PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; BORDER-TOP-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-COLOR: #888888; BORDER-LEFT-COLOR: #888888; BORDER-LEFT-WIDTH: 1px; PADDING-TOP: 0.4em"> </TD></TR><TR><TD style="PADDING-LEFT: 1em; BACKGROUND: #9cf" colSpan=4>[Book1]Active Projects</TD></TR></TBODY></TABLE>


Remark: this is just a simple parsing with little checking, but maybe it's enough. Please test it.
 
Upvote 0
BTW, I realized that the above code isn't perfect. If the ONLY thing in the INPUT cell is an IP address (e.g., "192.168.7.5"), then it doesn't work. It needs a non numeric after the last number to parse properly.

Examples:
INPUT: 192.168.5.49
OUTPUT: (null)

INPUT: 192.168.5.49(space)
OUTPUT: 192.168.5.49

INPUT: 192.168.4.3very odd
OUTPUT: 192.168.4.3

Not perfect, but I only need the code for a one-time parsing of about 1100 cells of long text. I can deal with this shortcoming, unless someone really has the urge to fix that, too. ;)
 
Upvote 0
Here's how I modified your code although pgc01 just blew up my brain. More stuff to learn!

Code:
Function FindIP(IP As String) As String
Dim cnt As Integer, NoOfDots As Integer
Dim i As Integer, c As Integer

IP = IP & " "
For i = 1 To Len(IP)
If IsNumeric(Mid(IP, i, 1)) Or Mid(IP, i, 1) = "." Then
cnt = cnt + 1
Else
'More than 6 consecutive char's that are
'either numeric or dot (".")
If cnt > 6 Then
FindIP = Mid(IP, i - cnt, cnt)

'Count the number of dot's
NoOfDots = 0
For c = 1 To Len(FindIP)
If Mid(FindIP, c, 1) = "." Then
NoOfDots = NoOfDots + 1
End If
Next c

'Check no of dots (s/b 3)
If NoOfDots >= 3 Then
    If Right(FindIP, 1) = "." Then FindIP = Left(FindIP, Len(FindIP) - 1)
    If Len(FindIP) > 0 Then FindIP = FindIP & ", " & FindIP(Right(IP, Len(IP) - i))
    If Right(FindIP, 2) = ", " Then FindIP = Left(FindIP, Len(FindIP) - 2)
    Exit Function
Else
FindIP = ""
End If

End If
cnt = 0
End If
Next i

End Function
 
Upvote 0
Hi sushinutnc
Welcome to the board

Try:
Code:
Function ExtractIPs(s As String)
 
With CreateObject("VBScript.RegExp")
    .Pattern = ".*?(\d{1,3}(\.\d{1,3}){3})|.*"
    .Global = True
    ExtractIPs = Replace(Trim(.Replace(s, " $1")), " ", ", ")
End With
End Function

Remark: this is just a simple parsing with little checking, but maybe it's enough. Please test it.

UNBELIEVABLE! THANK YOU, PGC! I've been googling and googling for RegEx support... Didn't realize VBA could handle it. THANK YOU!!!!!!!
 
Last edited:
Upvote 0
Not real elegant (like pgc01's)...but this should do it

Code:
Function FindIP(IP As String) As String
Dim cnt As Integer, NoOfDots As Integer
Dim i As Integer, c As Integer
myNewIP = ""
For i = 1 To Len(IP) + 1
If IsNumeric(Mid(IP, i, 1)) Or Mid(IP, i, 1) = "." Then
cnt = cnt + 1
Else
'More than 6 consecutive char's that are
'either numeric or dot (".")
If cnt > 6 Then
FindIP = Mid(IP, i - cnt, cnt)
If Right(FindIP, 1) = "." Then FindIP = Mid(FindIP, 1, Len(FindIP) - 1)

'Count the number of dot's
NoOfDots = 0
For c = 1 To Len(FindIP)
If Mid(FindIP, c, 1) = "." Then
NoOfDots = NoOfDots + 1
End If
Next c

'Check no of dots (s/b 3)
If NoOfDots = 3 Then
myNewIP = myNewIP & FindIP & ", "
Else
FindIP = ""
End If

End If
cnt = 0
End If
Next i
If Right(myNewIP, 2) = ", " Then FindIP = Mid(myNewIP, 1, Len(myNewIP) - 2)
End Function
Excel Workbook
AB
1We have a problem with 192.168.3.43 and also with 172.16.234.12. Also might have a problem with 10.168.4.1.192.168.3.43, 172.16.234.12, 10.168.4.1
2There is a system at 192.168.23.142, but it's not communicating with 192.168.4.132. Please call me at 555.432.2534 to see if it's talking to 10.4.22.3. My IP is 172.16.4.12. Thanks192.168.23.142, 192.168.4.132, 10.4.22.3, 172.16.4.12
3192.168.5.49192.168.5.49
4192.168.5.49192.168.5.49
5192.168.4.3very odd192.168.4.3
Sheet1
Excel 2007
Cell Formulas
RangeFormula
B1=findip(A1)
B2=findip(A2)
B3=findip(A3)
B4=findip(A4)
B5=findip(A5)
 
Upvote 0
Hey, PGC... It almost works perfectly, but also appears to parse certain non-printable chars that are part of my original dataset (a csv export from a database). Any ideas on how to fix that? (I'm going through the install of Jeanie... more to come).

Phox-- I'm getting the #VALUE! error with yours, whenver it finds an IP.

NoocH-- I'm not getting any response on yours...

I have to do some troubleshooting obviously.

Thanks everyone.
 
Upvote 0
It almost works perfectly, but also appears to parse certain non-printable chars that are part of my original dataset (a csv export from a database). Any ideas on how to fix that?

Can you post an example

Post a string where that happens, use for ex. a "#" in the place of the non-printable character and post its code.

You could also not pass those characters to the function, like

=ExtractIPs(CLEAN(A1))

but I would prefer to understand what's happening.
 
Upvote 0
Can you post an example

Post a string where that happens, use for ex. a "#" in the place of the non-printable character and post its code.

You could also not pass those characters to the function, like

=ExtractIPs(CLEAN(A1))

but I would prefer to understand what's happening.

Yep... Let me find a good example. I'd like to see if I can get it here with the Jeanie add-on and let you work with the actual non-printables. btw, when I use CLEAN, it returns with the #VALUE! error.
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,138
Members
449,098
Latest member
Doanvanhieu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top