Network administrators frequently struggle to accurately match mass IP addresses to their respective subnets within dynamic worksheets. While standard funding sources routinely cover enterprise IPAM software, manual Excel audits remain a necessity. Mastering this calculation grants teams immediate operational oversight without additional budget. Stipulation: Because Excel views IP addresses as text strings, we must first convert them into 32-bit numerical values for comparison. For example, parsing octets using LEFT, MID, and FIND functions serves as the standard methodology. Below, we will explore the precise formulas and logical steps required to automate your subnet validation.
Managing network inventories, analyzing firewall logs, or auditing IT assets often requires comparing individual IP addresses against specific subnets to find matches. However, because Excel treats IP addresses as text strings rather than numeric values, standard comparison operators like < or > fail to work out of the box.
To successfully determine whether an IP address falls within a specific Classless Inter-Domain Routing (CIDR) subnet (such as 192.168.1.0/24) in Excel, we must convert these IP addresses into numeric equivalents. This comprehensive guide details several methods to achieve this, ranging from modern dynamic array formulas to backward-compatible legacy solutions and VBA-based approaches.
An IPv4 address consists of four octets (e.g., A.B.C.D), where each octet represents an 8-bit number ranging from 0 to 255. To compare these mathematically, we convert them into a single 32-bit integer using the following mathematical formula:
Decimal Value = (A × 16,777,216) + (B × 65,536) + (C × 256) + D
Once both the target IP address and the subnet's boundaries (start and end IPs) are converted into these decimal values, checking for a match is a simple matter of verifying if the target IP's decimal value falls between the start and end decimals of the subnet.
If you are using Microsoft 365 or Excel 2021, you have access to powerful text-manipulation functions like TEXTSPLIT. This makes converting an IP address into its decimal value incredibly clean and easy.
Assuming your IP address is in cell A2, use the following formula to get its decimal equivalent:
=SUMPRODUCT(TEXTSPLIT(A2, ".") * {16777216, 65536, 256, 1})
How it works:
TEXTSPLIT(A2, ".") breaks the IP string at each period, returning an array of four strings: {"A", "B", "C", "D"}.{16777216, 65536, 256, 1}.SUMPRODUCT sums the multiplied values to output the final 32-bit decimal representation.To determine if a target IP (Cell A2) matches a subnet block (Cell B2, e.g., 192.168.1.0/24), we need to extract the network prefix, calculate the start and end range, and compare our target IP against those boundaries.
Using the LET function in Microsoft 365, we can write a single, self-contained formula to perform this check:
=LET(
target_ip, A2,
subnet, B2,
split_subnet, TEXTSPLIT(subnet, "/"),
subnet_ip, INDEX(split_subnet, 1),
prefix, VALUE(INDEX(split_subnet, 2)),
ip_dec, SUMPRODUCT(TEXTSPLIT(target_ip, ".") * {16777216, 65536, 256, 1}),
sub_dec, SUMPRODUCT(TEXTSPLIT(subnet_ip, ".") * {16777216, 65536, 256, 1}),
hosts, 2^(32 - prefix),
start_dec, BITAND(sub_dec, 4294967296 - hosts),
end_dec, start_dec + hosts - 1,
AND(ip_dec >= start_dec, ip_dec <= end_dec)
)
This formula returns TRUE if the IP in cell A2 matches the subnet in cell B2, and FALSE otherwise.
If you are working on older versions of Excel that do not support TEXTSPLIT or LET, you must use a traditional parsing formula. Parsing IPs without dynamic arrays requires finding the positions of the dots (periods) using nested SUBSTITUTE functions.
To convert an IP address in cell A2 to decimal in older Excel versions, use this robust formula:
=SUMPRODUCT(FILTERXML("<t><s>" & SUBSTITUTE(A2, ".", "</s><s>") & "</s></t>", "//s") * {16777216; 65536; 256; 1})
Note: FILTERXML is available in Excel 2013 and later on Windows. If you are on an even older version, or on Excel for Mac (which lacks FILTERXML), you must use a classic mathematical extraction formula:
=SUMPRODUCT(TRIM(MID(SUBSTITUTE(A2, ".", REPT(" ", 100)), {1, 101, 201, 301}, 100)) * {16777216, 65536, 256, 1})
Because writing a single-cell formula to parse CIDRs and run calculations in legacy Excel is incredibly long and prone to errors, the best practice is to set up a helper table:
| Column | Header / Description | Formula (Row 2) |
|---|---|---|
| A | Target IP | 192.168.1.55 (User input) |
| B | Target Decimal Value | =SUMPRODUCT(TRIM(MID(SUBSTITUTE(A2, ".", REPT(" ", 100)), {1,101,201,301}, 100)) * {16777216,65536,256,1}) |
| C | Subnet (CIDR) | 192.168.1.0/24 (User input) |
| D | Subnet IP Decimal | =SUMPRODUCT(TRIM(MID(SUBSTITUTE(LEFT(C2, FIND("/", C2)-1), ".", REPT(" ", 100)), {1,101,201,301}, 100)) * {16777216,65536,256,1}) |
| E | Prefix Value | =VALUE(MID(C2, FIND("/", C2)+1, LEN(C2))) |
| F | Start Decimal of Subnet | =BITAND(D2, 4294967296 - 2^(32-E2)) |
| G | End Decimal of Subnet | =F2 + 2^(32-E2) - 1 |
| H | Is Match? (Result) | =AND(B2 >= F2, B2 <= G2) |
If you are allowed to use macro-enabled workbooks (.xlsm), using a User Defined Function (UDF) is by far the cleanest and most manageable approach. Instead of writing complex formulas, you can simply type =IsIpInSubnet(A2, B2) in your worksheet.
ALT + F11 to open the VBA Editor.Insert > Module from the top menu.Function IsIpInSubnet(ByVal targetIP As String, ByVal cidrSubnet As String) As Variant
On Error GoTo ErrorHandler
Dim parts() As String
Dim subnetParts() As String
Dim subnetIP As String
Dim prefix As Integer
Dim targetDec As Double, subnetDec As Double
Dim maskDec As Double, startDec As Double, endDec As Double
' Split CIDR subnet into IP and prefix
parts = Split(cidrSubnet, "/")
If UBound(parts) <> 1 Then
IsIpInSubnet = CVErr(xlErrVal)
Exit Function
End If
subnetIP = parts(0)
prefix = CInt(parts(1))
' Validate prefix range
If prefix < 0 Or prefix > 32 Then
IsIpInSubnet = CVErr(xlErrVal)
Exit Function
End If
' Convert IPs to decimal values
targetDec = IpToDecimal(targetIP)
subnetDec = IpToDecimal(subnetIP)
' Calculate network range bounds
If prefix = 32 Then
IsIpInSubnet = (targetDec = subnetDec)
Exit Function
End If
maskDec = CDbl(2 ^ 32 - 2 ^ (32 - prefix))
' Perform bitwise AND logic to find the network boundaries
startDec = DecimalBitwiseAnd(subnetDec, maskDec)
endDec = startDec + (2 ^ (32 - prefix)) - 1
' Evaluate if target falls within calculated boundary range
If targetDec >= startDec And targetDec <= endDec Then
IsIpInSubnet = True
Else
IsIpInSubnet = False
End If
Exit Function
ErrorHandler:
IsIpInSubnet = CVErr(xlErrValue)
End Function
Private Function IpToDecimal(ByVal ip As String) As Double
Dim octets() As String
octets = Split(ip, ".")
If UBound(octets) <> 3 Then Err.Raise 9
IpToDecimal = (CDbl(octets(0)) * 16777216) + (CDbl(octets(1)) * 65536) + (CDbl(octets(2)) * 256) + CDbl(octets(3))
End Function
Private Function DecimalBitwiseAnd(ByVal num1 As Double, ByVal num2 As Double) As Double
' Emulates 32-bit unsigned bitwise AND using double precision math
Dim i As Integer
Dim power As Double
Dim result As Double
result = 0
For i = 31 To 0 Step -1
power = 2 ^ i
If num1 >= power And num2 >= power Then
result = result + power
num1 = num1 - power
num2 = num2 - power
Else
If num1 >= power Then num1 = num1 - power
If num2 >= power Then num2 = num2 - power
End If
Next i
DecimalBitwiseAnd = result
End Function
Close the VBA Editor and return to your sheet. You can now run checks effortlessly:
=IsIpInSubnet(A2, "10.0.0.0/8")
LET functions inside every row. Instead, calculate the CIDR range decimal limits in a separate table once, and then use simple numeric IF / AND statements to reference them.TRIM function on IP input cells to prevent lookup errors.Choosing the correct method depends on your Excel configuration and environment rules:
LET dynamic array formula containing TEXTSPLIT. It is clean, doesn't require VBA, and is easy to debug.FILTERXML or nested TRIM/MID string splitting.
Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.