jay_waugh
asked on
Is an IP address in a Range
Hi
I have a spreadsheet that contains a list of IP addresses. I want to work out whether or not the values in this list of IPs are in any of the IP address ranges that I have on another worksheet using VBA and I can't get it to work.
I have created some code that uses the CIDR range to identify the first and last IP addresses in the range. I am then running a further loop to check if my queried IP is between those two values and I can't get it to work.
Can anyone help?
e.g. queried IP address = 1.1.1.8
start IP address in range is 1.1.1.1 and end IP address in range is 1.1.1.254
I run an if command in a loop something like:-
dim targetIP as int
dim startipaddress as int
dim endipaddress as int
If targetIP >= startipaddress and targetIP <= endipaddress then
1
else
0
End if
I have tried using int, variant and Long values and none seem to work.
What am I doing wrong?
Thanks
I have a spreadsheet that contains a list of IP addresses. I want to work out whether or not the values in this list of IPs are in any of the IP address ranges that I have on another worksheet using VBA and I can't get it to work.
I have created some code that uses the CIDR range to identify the first and last IP addresses in the range. I am then running a further loop to check if my queried IP is between those two values and I can't get it to work.
Can anyone help?
e.g. queried IP address = 1.1.1.8
start IP address in range is 1.1.1.1 and end IP address in range is 1.1.1.254
I run an if command in a loop something like:-
dim targetIP as int
dim startipaddress as int
dim endipaddress as int
If targetIP >= startipaddress and targetIP <= endipaddress then
1
else
0
End if
I have tried using int, variant and Long values and none seem to work.
What am I doing wrong?
Thanks
Can you post a sample workbook to work with?
I believe that you'll have to parse the IP address into 4 integers and then do your comparisons.
ASKER
Can you pease confirm what you meant by "parse"?
I tried a Cint() round the value and that didn't work.
I tried a Cint() round the value and that didn't work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, your code executes perfectly but it doesn't seem to return any "false" answers to the >= < query for me.
Did it work when you tested it?
Did it work when you tested it?
It won't trigger a msgbox but you need to check it for True in your sub routine like this
To check if the condition is not met i.e. for False like this...
Or in the immediate window you may try like this....
If IsIPFound(TargetIP As String, StartIP As String, EndIP As String) Then
'Do all the stuff if Function returns True
End IF
To check if the condition is not met i.e. for False like this...
If Not IsIPFound(TargetIP As String, StartIP As String, EndIP As String) Then
'Do all the stuff if Function returns False
End If
Or in the immediate window you may try like this....
MsgBox IsIPFound(TargetIP As String, StartIP As String, EndIP As String)
ASKER
Thanks. Ok I'll try that and let you know.
ASKER
Thanks again and I tried your code but am sadly still having problems. This is down to the fact that some of my end IP addresses exceed 10 characters and as such the "Long" variable is reporting an overflow error.
Is there anything that I can I do to get around this?
Is there anything that I can I do to get around this?
All valid IP addresses consist of 4 parts each separated by a dot. If that condition is met, the code will work for you.
Is it possible for you to upload a sample workbook with some of IP addresses to see what the problem is?
Is it possible for you to upload a sample workbook with some of IP addresses to see what the problem is?
ASKER
:-) I can't post real IP addresses.
For me, the line where you take out the full stops fails for a value such as 123.456.123.456 as there are still 12 separate characters involved once the full stops have been removed.
123456123456
The Int or long variable types don't seem to be able to handle this.
For me, the line where you take out the full stops fails for a value such as 123.456.123.456 as there are still 12 separate characters involved once the full stops have been removed.
123456123456
The Int or long variable types don't seem to be able to handle this.
Code is not getting the whole IP address after removing the dots into one variable, so that doesn't matter if the variables have the int datatype.
The variables get the last part i.e. part right to the last dot in the address.
Can you post one example which failed at your end (after changing the IP addresses in the function arguments)?
The variables get the last part i.e. part right to the last dot in the address.
Can you post one example which failed at your end (after changing the IP addresses in the function arguments)?
ASKER
Sorry I should have said, that I have changed the code slightly as your version only seemed to return the last octet of the IP address and I need to compare the whole subnet.
tIP = Val(WorksheetFunction.Repl ace(Target IP, 1, InStrRev(TargetIP, "."), ""))
sIP = Val(WorksheetFunction.Repl ace(StartI P, 1, InStrRev(StartIP, "."), ""))
eIP = Val(WorksheetFunction.Repl ace(EndIP, 1, InStrRev(EndIP, "."), ""))
I have changed to:
tIP = CLng(Application.Substitut e(TargetIP , ".", ""))
sIP = CLng(Application.Substitut e(StartIP, ".", ""))
eIP = CLng(Application.Substitut e(EndIP, ".", ""))
And as previously stated this is causing me the issue where I have too many characters for a Long and Integer variable type so am experiencing the "overflow" error.
Thanks for your continued engagement.... Do you have any further ideas on how I can resolve this issue?
tIP = Val(WorksheetFunction.Repl
sIP = Val(WorksheetFunction.Repl
eIP = Val(WorksheetFunction.Repl
I have changed to:
tIP = CLng(Application.Substitut
sIP = CLng(Application.Substitut
eIP = CLng(Application.Substitut
And as previously stated this is causing me the issue where I have too many characters for a Long and Integer variable type so am experiencing the "overflow" error.
Thanks for your continued engagement.... Do you have any further ideas on how I can resolve this issue?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks....I keep getting a type mismatch now. Can you see it, I don't seem to be able to.
Dim LUIPaddress As String
Sub IpInRange()
Dim TotNumofRows As Integer
Dim Numofrows As Integer
TotNumofRows = (Range("A:A").Cells.Specia lCells(xlC ellTypeCon stants).Co unt)
For Numofrows = 2 To TotNumofRows
rngletter = "B"
LUIPaddress = Range(rngletter & Numofrows).Value
CompareSubnet
If Refdataok > 0 Then
Range(rngletter & Numofrows).Cells.Interior. ColorIndex = 4
Else
Range(rngletter & Numofrows).Cells.Interior. ColorIndex = 3
End If
Next
Sub CompareSubnet()
Dim refdata As Boolean
Dim RefDataNumberofRows As Integer
Dim StIP As Integer
Dim EnIP As Integer
Dim RefDataTotNumberofRows As Integer
Refdataok = 0
Worksheets("UDSubnets").Ac tivate
RefDataTotNumberofRows = (Range("A:A").Cells.Specia lCells(xlC ellTypeCon stants).Co unt)
For RefDataNumberofRows = 2 To RefDataTotNumberofRows
StIP = Range("G" & RefDataNumberofRows).Value
EnIP = Range("H" & RefDataNumberofRows).Value
Next
End Sub
Function IsIPFound(TargetIP As String, StartIP As String, EndIP As String) As Boolean
Dim tIP As Double, sIP As Double, eIP As Double
tIP = CDbl(Replace(TargetIP, ".", ""))
sIP = CDbl(Replace(StartIP, ".", ""))
eIP = CDbl(Replace(EndIP, ".", ""))
IsIPFound = (tIP >= sIP And tIP <= eIP)
End Function
Sub HomeWorkbook()
Worksheets("TargetIPs").Ac tivate
End Sub
Dim LUIPaddress As String
Sub IpInRange()
Dim TotNumofRows As Integer
Dim Numofrows As Integer
TotNumofRows = (Range("A:A").Cells.Specia
For Numofrows = 2 To TotNumofRows
rngletter = "B"
LUIPaddress = Range(rngletter & Numofrows).Value
CompareSubnet
If Refdataok > 0 Then
Range(rngletter & Numofrows).Cells.Interior.
Else
Range(rngletter & Numofrows).Cells.Interior.
End If
Next
Sub CompareSubnet()
Dim refdata As Boolean
Dim RefDataNumberofRows As Integer
Dim StIP As Integer
Dim EnIP As Integer
Dim RefDataTotNumberofRows As Integer
Refdataok = 0
Worksheets("UDSubnets").Ac
RefDataTotNumberofRows = (Range("A:A").Cells.Specia
For RefDataNumberofRows = 2 To RefDataTotNumberofRows
StIP = Range("G" & RefDataNumberofRows).Value
EnIP = Range("H" & RefDataNumberofRows).Value
Next
End Sub
Function IsIPFound(TargetIP As String, StartIP As String, EndIP As String) As Boolean
Dim tIP As Double, sIP As Double, eIP As Double
tIP = CDbl(Replace(TargetIP, ".", ""))
sIP = CDbl(Replace(StartIP, ".", ""))
eIP = CDbl(Replace(EndIP, ".", ""))
IsIPFound = (tIP >= sIP And tIP <= eIP)
End Function
Sub HomeWorkbook()
Worksheets("TargetIPs").Ac
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just a note:
Correct me if I'm wrong, but I see a problem with Subodh's solution in some cases ... it's in the
I've exteded my Excel workbook with that conversion by means of excel formula to show that effect.
To circumvent that effect w/o using binary conversion like my example, you'll have to split the IP into parts, format them to a 3-digit string with leading zeroes, concatenate them and then convert that to a number.
IsIPinRange_2.xlsm
Correct me if I'm wrong, but I see a problem with Subodh's solution in some cases ... it's in the
... = Val(WorksheetFunction.Replace(TargetIP, 1, InStrRev(TargetIP, "."), ""))
part. It works well, if the numbers in the parts of the IP's have the same number of digits in start, end and target. But if there are differences, esp. when checking areas where not only the last part of the IP changes, that way goes wrong sometimes.I've exteded my Excel workbook with that conversion by means of excel formula to show that effect.
To circumvent that effect w/o using binary conversion like my example, you'll have to split the IP into parts, format them to a 3-digit string with leading zeroes, concatenate them and then convert that to a number.
IsIPinRange_2.xlsm
@Frank
You are referring to the earlier version of the code where only the last part was being compared.
Please see the latest code in Post ID: 41727114.
You are referring to the earlier version of the code where only the last part was being compared.
Please see the latest code in Post ID: 41727114.
@Subodh
As far as I'm referring to the earlier code, you're right.
Your last code post (41727114) gives (as far as I see) a correction to the asker's code post 41727142, and that post uses (basically) the same technique for comparison, where simply the dots are stripped out of the IP address string.
I presume my point is valid on that, too, besides on the fact that the code in the accepted answer (41725084, which referred to) is easier to read and contains only the core function of the compare.
Have you tried my example workbook ?
(No challenge for points, anyhow ... just mentionig some suspected weakness)
As far as I'm referring to the earlier code, you're right.
Your last code post (41727114) gives (as far as I see) a correction to the asker's code post 41727142, and that post uses (basically) the same technique for comparison, where simply the dots are stripped out of the IP address string.
I presume my point is valid on that, too, besides on the fact that the code in the accepted answer (41725084, which referred to) is easier to read and contains only the core function of the compare.
Have you tried my example workbook ?
(No challenge for points, anyhow ... just mentionig some suspected weakness)
No I didn't try your solution as I am having issue here opening the downloaded macro files without saving them.
And yes you are right the asker chose an irrelevant answer as a best solution as it was tweaked by the asker and later my me. You never know if your proposed code has been tweaked by the asker unless you are informed about that which the asker did in Post ID: 41727107.
And yes you are right the asker chose an irrelevant answer as a best solution as it was tweaked by the asker and later my me. You never know if your proposed code has been tweaked by the asker unless you are informed about that which the asker did in Post ID: 41727107.
I see ...
OK - no problem on my side.
I've stated my point about the problem I see and any subsequent reader will get the info if needed ... have a nice day :)
OK - no problem on my side.
I've stated my point about the problem I see and any subsequent reader will get the info if needed ... have a nice day :)
Yes your point was correct.
Thank you! you too. :)
Thank you! you too. :)