Validating IP Addresses in VBA

Validating an IP address is an interesting exercise. Per RFC 1918, IP addresses are in the following format:

nnn.nnn.nnn.nnn

In other words, four blocks of up to three numbers each.

Validating the format can be done using the following regex:

[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}

There are a few constraints for public networks, however.

  1. The first number cannot be 10.
  2. If the first number is 172, the second number cannot be between 16 and 31 (inclusive).
  3. If the first number is 192, the second number cannot be 168.
  4. None of the numbers can be greater than 255.

These address blocks are reserved for private networks. Each of these conditions can be addressed with a simple Select Case statement (and the Split function) in an encapsulated function.

Existing Examples

The existing code I found merely validates the nnn.nnn.nnn.nnn format without checking the other constraints. Most validation code seems to use manual parsing to validate the IP format, and only tries to satisfy condition #4. Technically speaking, closed networks that are off the grid can use any IP they like; pretty much everybody's router uses 192.168.1.1 without issue. But we want to be RFC compliant as well!

Validate IP Address using RFC 1918 Criteria

The following function will validate IP addresses using RegEx methods. Then we will proceed to walk through the above criteria to ensure a valid address.

Function IsValidIPAddress(ipaddress As String) As Boolean
  Dim regex As Object  ' RegExp
  Dim quadValues() As String
  Dim firstValue As Long
  Dim secondValue As Long
  Dim thirdValue As Long
  Dim fourthValue As Long

  Const testpattern As String = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"

  Set regex = GetRegEx
  If regex Is Nothing Then Exit Function

  IsValidIPAddress = TestRegEx(regex, testpattern, ipaddress)

  If IsValidIPAddress Then
    ' tokenize IP address into array elements
    quadValues = StringToArray(ipaddress, ".")
    firstValue = CLng(quadValues(0))
    secondValue = CLng(quadValues(1))
    thirdValue = CLng(quadValues(2))
    fourthValue = CLng(quadValues(3))

    Select Case True
      ' The first number cannot be 10.
    Case firstValue = 10
      IsValidIPAddress = Not IsValidIPAddress
      ' If the first number is 172, the second number
      ' cannot be between 16 and 31 (inclusive)
    Case firstValue = 172
      If secondValue >= 16 Then
        If secondValue <= 31 Then
          IsValidIPAddress = Not IsValidIPAddress
        End If
      End If
      ' If the first number is 192, the second number
      ' cannot be 168.
    Case firstValue = 192
      If secondValue = 168 Then
        IsValidIPAddress = Not IsValidIPAddress
      End If
      ' None of the numbers can be greater than 255.
    Case firstValue > 255
      If secondValue > 255 Then
        If thirdValue > 255 Then
          If fourthValue > 255 Then
            IsValidIPAddress = Not IsValidIPAddress
          End If
        End If
      End If
    End Select
  End If

End Function

The GetRegEx and TestRegEx functions may be found at Regular Expressions Testing in VBA. The StringToArray function may be found at Convert a String to an Array.

Site last updated: May 17, 2012

Excel School