How do I determine if IP is in IP Range?

Posted on 2008-10-07
Last Modified: 2012-06-27
I want to check to see if an IP is in  a supplied range of IP's. For example, is the IP in the range of - How could i do that with

Question by:ronayers
  • 2
  • 2
LVL 19

Expert Comment

by:Stephen Manderson
Comment Utility
Hi There here is one way of doing it..
                   ' Start                   'Finish               'Check For
IsBetween(" ", "", "") Returns False
IsBetween(" ", "", "") Returns True


    Public Function IsBetween(ByVal Start As String, ByVal Finish As String, ByVal CheckFor As String) As Boolean

        Dim StartRange As Long = CLng(Start.Replace(".", ""))

        Dim FinishRange As Long = CLng(Finish.Replace(".", ""))

        Dim CheckForIP As Long = CLng(CheckFor.Replace(".", ""))

        If StartRange >= CheckForIP Or CheckForIP <= FinishRange Then

            Return True


            Return False

        End If

    End Function

Open in new window

LVL 19

Expert Comment

by:Stephen Manderson
Comment Utility
Need to change the 'Or' in line 7 to 'And'

Expert Comment

Comment Utility
The posted code will not work properly because would be consider in-range when it is not. Try this code instead:

Author Comment

Comment Utility

that will just tell me the subnet the IPs are own based off a range. That will not solve my problem. Any other ideas?


Accepted Solution

troyw1 earned 500 total points
Comment Utility
I tried to find the example code I have used before, but I can't find it and I no longer access to the customer code that I wrote. It was a function that converted IP addresses to their correct integer value.

Here is another interesting approach that should work also; comparing each octet range, one at a time.
'Set the values to test

RangeMin = ""

RangeMax = ""

TestIP = ""

'Split the above values into arrays

MinAry = Split(RangeMin, ".", -1, 1)

MaxAry = Split(RangeMax, ".", -1, 1)

IPAry = Split(TestIP, ".", -1, 1)

'Test the octets

error = ""

If IPAry(0) < MinAry(0) OR IPAry(0) > MaxAry(0) Then

error = "First octect out of range"

ElseIF IPAry(1) < MinAry(1) OR IPAry(1) > MaxAry(1) Then

error = "Second octect out of range"

ElseIF IPAry(2) < MinAry(2) OR IPAry(2) > MaxAry(2) Then

error = "Third octect out of range"

ElseIF IPAry(3) < MinAry(3) OR IPAry(3) > MaxAry(3) Then

error = "Fourth octect out of range"

End If

'Display the results

If error<>"" Then

MsgBox error, 16, "Processing Complete"


MsgBox "The IP is within range", 1, "Processing Complete"

End If

Open in new window


Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now