Expanding IP ranges thru 3 octets

Danny54

Active Member
Joined
Jul 3, 2019
Messages
295
Office Version
  1. 365
Platform
  1. Windows
Good Morning

I'm trying to modify the following VBA code in order to expand the values found in a ip range

The vba coded currently works successfully with the 4th octet
ie. 10.10.10.1-10.10.10.3 would be expanded to show 10.10.10.1, 10.10.10.2, 10.10.10.3
ie. 10.10.10.1-10.10.10.3, 10.10.20.5-10.10.20.6 would be expanded to show 10.10.10.1, 10.10.10.2, 10.10.10.3 , 10.10.20.5, 10.10.20.6

Note Octet information
10.20.30.1 = octet are counted from left to right. (this example show 10 is the 1st octet, 20 is second octet, 30 is the 3rd octet and 1 is the 4th octet)
each octet is counted from 0-255
once we hit 255 in any octet the next increased ip would be using the octet to the left .. 10.10.10.255, 10.10.11.0, 10.10.11.1

The code needs to be modified to work with the 3rd octet and potentially the 4th

ie... For the following ip range 10.10.10.1-10.10.11.255 should be expanded to show 10.10.10.1, .... 10.10.10.255 .... 10.10.11.0, .... 10.10.11,255


Code:
Sub doIt()
Dim rng As Range
Dim cll As Range
Dim commaSeparated
Dim dashSeparated
Dim commaItem
Dim bytes
Dim lastByteFrom As Double
Dim firstThreeBytes As String
Dim lastByteTo As Double
Dim i As Double
Dim result() As String

Range("C1").ClearContents

Set rng = Selection ' select all values in column B
For Each cll In rng.Cells
ReDim result(0)
commaSeparated = Split(cll.Value, ",")

For Each commaItem In commaSeparated
dashSeparated = Split(Trim(commaItem), "-")

If UBound(dashSeparated) = 1 Then
bytes = Split(dashSeparated(0), ".")
lastByteFrom = bytes(3)
ReDim Preserve bytes(2)
firstThreeBytes = Join(bytes, ".")
lastByteTo = Split(dashSeparated(1), ".")(3)

For i = 0 To lastByteTo - lastByteFrom
ReDim Preserve result(UBound(result) + 1)
result(UBound(result)) = firstThreeBytes & "." & (lastByteFrom + i)
Next i

Else
ReDim Preserve result(UBound(result) + 1)
result(UBound(result)) = Trim(commaItem)
End If

Next commaItem
If UBound(commaSeparated) > -1 Then
For i = 1 To UBound(result)
result(i - 1) = result(i)
Next i
ReDim Preserve result(UBound(result) - 1)
cll.Offset(, 1).Value = Join(result, ", ")
End If

Next cll
End Sub


Test Data
A10.10.10.0
B10.10.10.0-10.10.10.2
C10.10.10.0-10.10.10.255
D10.10.10.0-10.10.11.0
E10.10.10.0-10.11.11.0
F10.15.1.0-10.15.1.5,10.15.2.3-10.15.2.5


Output from the VBA Script above

CompanyIP RangeResults from VBA ScriptDescription of Results
A10.10.10.010.10.10.0ok
B10.10.10.0-10.10.10.210.10.10.0, 10.10.10.1, 10.10.10.2ok
C10.10.10.0-10.10.10.25510.10.10.0, 10.10.10.1, 10.10.10.2, 10.10.10.3, 10.10.10.4, 10.10.10.5, 10.10.10.6, 10.10.10.7, 10.10.10.8, 10.10.10.9, 10.10.10.10, 10.10.10.11, 10.10.10.12, 10.10.10.13, 10.10.10.14, 10.10.10.15, 10.10.10.16, 10.10.10.17, 10.10.10.18, 10.10.10.19, 10.10.10.20, 10.10.10.21, 10.10.10.22, 10.10.10.23, 10.10.10.24, 10.10.10.25, 10.10.10.26, 10.10.10.27, 10.10.10.28, 10.10.10.29, 10.10.10.30, 10.10.10.31, 10.10.10.32, 10.10.10.33, 10.10.10.34, 10.10.10.35, 10.10.10.36, 10.10.10.37, 10.10.10.38, 10.10.10.39, 10.10.10.40, 10.10.10.41, 10.10.10.42, 10.10.10.43, 10.10.10.44, 10.10.10.45, 10.10.10.46, 10.10.10.47, 10.10.10.48, 10.10.10.49, 10.10.10.50, 10.10.10.51, 10.10.10.52, 10.10.10.53, 10.10.10.54, 10.10.10.55, 10.10.10.56, 10.10.10.57, 10.10.10.58, 10.10.10.59, 10.10.10.60, 10.10.10.61, 10.10.10.62, 10.10.10.63, 10.10.10.64, 10.10.10.65, 10.10.10.66, 10.10.10.67, 10.10.10.68, 10.10.10.69, 10.10.10.70, 10.10.10.71, 10.10.10.72, 10.10.10.73, 10.10.10.74, 10.10.10.75, 10.10.10.76, 10.10.10.77, 10.10.10.78, 10.10.10.79, 10.10.10.80, 10.10.10.81, 10.10.10.82, 10.10.10.83, 10.10.10.84, 10.10.10.85, 10.10.10.86, 10.10.10.87, 10.10.10.88, 10.10.10.89, 10.10.10.90, 10.10.10.91, 10.10.10.92, 10.10.10.93, 10.10.10.94, 10.10.10.95, 10.10.10.96, 10.10.10.97, 10.10.10.98, 10.10.10.99, 10.10.10.100, 10.10.10.101, 10.10.10.102, 10.10.10.103, 10.10.10.104, 10.10.10.105, 10.10.10.106, 10.10.10.107, 10.10.10.108, 10.10.10.109, 10.10.10.110, 10.10.10.111, 10.10.10.112, 10.10.10.113, 10.10.10.114, 10.10.10.115, 10.10.10.116, 10.10.10.117, 10.10.10.118, 10.10.10.119, 10.10.10.120, 10.10.10.121, 10.10.10.122, 10.10.10.123, 10.10.10.124, 10.10.10.125, 10.10.10.126, 10.10.10.127, 10.10.10.128, 10.10.10.129, 10.10.10.130, 10.10.10.131, 10.10.10.132, 10.10.10.133, 10.10.10.134, 10.10.10.135, 10.10.10.136, 10.10.10.137, 10.10.10.138, 10.10.10.139, 10.10.10.140, 10.10.10.141, 10.10.10.142, 10.10.10.143, 10.10.10.144, 10.10.10.145, 10.10.10.146, 10.10.10.147, 10.10.10.148, 10.10.10.149, 10.10.10.150, 10.10.10.151, 10.10.10.152, 10.10.10.153, 10.10.10.154, 10.10.10.155, 10.10.10.156, 10.10.10.157, 10.10.10.158, 10.10.10.159, 10.10.10.160, 10.10.10.161, 10.10.10.162, 10.10.10.163, 10.10.10.164, 10.10.10.165, 10.10.10.166, 10.10.10.167, 10.10.10.168, 10.10.10.169, 10.10.10.170, 10.10.10.171, 10.10.10.172, 10.10.10.173, 10.10.10.174, 10.10.10.175, 10.10.10.176, 10.10.10.177, 10.10.10.178, 10.10.10.179, 10.10.10.180, 10.10.10.181, 10.10.10.182, 10.10.10.183, 10.10.10.184, 10.10.10.185, 10.10.10.186, 10.10.10.187, 10.10.10.188, 10.10.10.189, 10.10.10.190, 10.10.10.191, 10.10.10.192, 10.10.10.193, 10.10.10.194, 10.10.10.195, 10.10.10.196, 10.10.10.197, 10.10.10.198, 10.10.10.199, 10.10.10.200, 10.10.10.201, 10.10.10.202, 10.10.10.203, 10.10.10.204, 10.10.10.205, 10.10.10.206, 10.10.10.207, 10.10.10.208, 10.10.10.209, 10.10.10.210, 10.10.10.211, 10.10.10.212, 10.10.10.213, 10.10.10.214, 10.10.10.215, 10.10.10.216, 10.10.10.217, 10.10.10.218, 10.10.10.219, 10.10.10.220, 10.10.10.221, 10.10.10.222, 10.10.10.223, 10.10.10.224, 10.10.10.225, 10.10.10.226, 10.10.10.227, 10.10.10.228, 10.10.10.229, 10.10.10.230, 10.10.10.231, 10.10.10.232, 10.10.10.233, 10.10.10.234, 10.10.10.235, 10.10.10.236, 10.10.10.237, 10.10.10.238, 10.10.10.239, 10.10.10.240, 10.10.10.241, 10.10.10.242, 10.10.10.243, 10.10.10.244, 10.10.10.245, 10.10.10.246, 10.10.10.247, 10.10.10.248, 10.10.10.249, 10.10.10.250, 10.10.10.251, 10.10.10.252, 10.10.10.253, 10.10.10.254, 10.10.10.255ok
D10.10.10.0-10.10.11.010.10.10.0doesn’t show correct values
E10.10.10.0-10.11.11.010.10.10.0doesn’t show correct values
F10.15.1.0-10.15.1.5,10.15.2.3-10.15.2.510.15.1.0, 10.15.1.1, 10.15.1.2, 10.15.1.3, 10.15.1.4, 10.15.1.5, 10.15.2.3, 10.15.2.4, 10.15.2.5ok
G
10.25.25.127- 10.25.26.3
throws subscript out of range because ending 4th octet in second range is lower than 4th octet in first range.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The following code seems to work with the test data you provided.

VBA Code:
Sub ExpandIP()
Application.ScreenUpdating = False
Dim r As Range:         Set r = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim SP() As String
Dim IP() As String

With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(AR)
        If InStr(AR(i, 1), "-") > 0 Then
            SP = Split(AR(i, 1), ",")
            For j = 0 To UBound(SP)
                IP = Split(SP(j), "-")
                .Add CountIP(IP(0), IP(1)), 1
            Next j
        Else
            .Add AR(i, 1), 1
        End If
        Cells(i, 2).Value = Join(.keys, ", ")
        .RemoveAll
    Next i
End With

Application.ScreenUpdating = True
End Sub

Function CountIP(SIP As String, EIP As String)
Dim IP1() As String:    IP1 = Split(SIP, ".")
Dim IP2() As String:    IP2 = Split(EIP, ".")
Dim b As Boolean:       b = True

With CreateObject("System.Collections.ArrayList")
    .Add Join(IP1, ".")
    Do Until Join(IP1, ".") = Join(IP2, ".")
        b = True
        For i = 1 To 3
            If IP1(i) = 255 Then
                IP1(i - 1) = IP1(i - 1) + 1
                IP1(i) = 0
                b = False
            End If
        Next i
        If b Then IP1(3) = IP1(3) + 1
        .Add Join(IP1, ".")
    Loop
    CountIP = Join(.toArray, ", ")
End With
End Function
 
Upvote 0
Thanks so much for your help. I tried the script.
I think I can work out the issues.

Two things i've noticed.
The script as written expects to find the IP's in column A not B. If I move the stuff from column B to A then the expansion works but my company is missing
Second, the first row where there is only one IP listed, it doesnt make it to the output causing all values to be moved up which doesn't match the values that is being expanded

:)
 
Upvote 0
Updated code and results below.

ExpandIPIII.xlsm
ABC
1CompanyIP RangeResults from VBA Script
2A10.10.10.010.10.10.0
3B10.10.10.0-10.10.10.210.10.10.0, 10.10.10.1, 10.10.10.2
4C10.10.10.0-10.10.10.25510.10.10.0, 10.10.10.1, 10.10.10.2, 10.10.10.3, 10.10.10.4, 10.10.10.5, 10.10.10.6, 10.10.10.7, 10.10.10.8, 10.10.10.9, 10.10.10.10, 10.10.10.11, 10.10.10.12, 10.10.10.13, 10.10.10.14, 10.10.10.15, 10.10.10.16, 10.10.10.17, 10.10.10.18, 10.10.10.19, 10.10.10.20, 10.10.10.21, 10.10.10.22, 10.10.10.23, 10.10.10.24, 10.10.10.25, 10.10.10.26, 10.10.10.27, 10.10.10.28, 10.10.10.29, 10.10.10.30, 10.10.10.31, 10.10.10.32, 10.10.10.33, 10.10.10.34, 10.10.10.35, 10.10.10.36, 10.10.10.37, 10.10.10.38, 10.10.10.39, 10.10.10.40, 10.10.10.41, 10.10.10.42, 10.10.10.43, 10.10.10.44, 10.10.10.45, 10.10.10.46, 10.10.10.47, 10.10.10.48, 10.10.10.49, 10.10.10.50, 10.10.10.51, 10.10.10.52, 10.10.10.53, 10.10.10.54, 10.10.10.55, 10.10.10.56, 10.10.10.57, 10.10.10.58, 10.10.10.59, 10.10.10.60, 10.10.10.61, 10.10.10.62, 10.10.10.63, 10.10.10.64, 10.10.10.65, 10.10.10.66, 10.10.10.67, 10.10.10.68, 10.10.10.69, 10.10.10.70, 10.10.10.71, 10.10.10.72, 10.10.10.73, 10.10.10.74, 10.10.10.75, 10.10.10.76, 10.10.10.77, 10.10.10.78, 10.10.10.79, 10.10.10.80, 10.10.10.81, 10.10.10.82, 10.10.10.83, 10.10.10.84, 10.10.10.85, 10.10.10.86, 10.10.10.87, 10.10.10.88, 10.10.10.89, 10.10.10.90, 10.10.10.91, 10.10.10.92, 10.10.10.93, 10.10.10.94, 10.10.10.95, 10.10.10.96, 10.10.10.97, 10.10.10.98, 10.10.10.99, 10.10.10.100, 10.10.10.101, 10.10.10.102, 10.10.10.103, 10.10.10.104, 10.10.10.105, 10.10.10.106, 10.10.10.107, 10.10.10.108, 10.10.10.109, 10.10.10.110, 10.10.10.111, 10.10.10.112, 10.10.10.113, 10.10.10.114, 10.10.10.115, 10.10.10.116, 10.10.10.117, 10.10.10.118, 10.10.10.119, 10.10.10.120, 10.10.10.121, 10.10.10.122, 10.10.10.123, 10.10.10.124, 10.10.10.125, 10.10.10.126, 10.10.10.127, 10.10.10.128, 10.10.10.129, 10.10.10.130, 10.10.10.131, 10.10.10.132, 10.10.10.133, 10.10.10.134, 10.10.10.135, 10.10.10.136, 10.10.10.137, 10.10.10.138, 10.10.10.139, 10.10.10.140, 10.10.10.141, 10.10.10.142, 10.10.10.143, 10.10.10.144, 10.10.10.145, 10.10.10.146, 10.10.10.147, 10.10.10.148, 10.10.10.149, 10.10.10.150, 10.10.10.151, 10.10.10.152, 10.10.10.153, 10.10.10.154, 10.10.10.155, 10.10.10.156, 10.10.10.157, 10.10.10.158, 10.10.10.159, 10.10.10.160, 10.10.10.161, 10.10.10.162, 10.10.10.163, 10.10.10.164, 10.10.10.165, 10.10.10.166, 10.10.10.167, 10.10.10.168, 10.10.10.169, 10.10.10.170, 10.10.10.171, 10.10.10.172, 10.10.10.173, 10.10.10.174, 10.10.10.175, 10.10.10.176, 10.10.10.177, 10.10.10.178, 10.10.10.179, 10.10.10.180, 10.10.10.181, 10.10.10.182, 10.10.10.183, 10.10.10.184, 10.10.10.185, 10.10.10.186, 10.10.10.187, 10.10.10.188, 10.10.10.189, 10.10.10.190, 10.10.10.191, 10.10.10.192, 10.10.10.193, 10.10.10.194, 10.10.10.195, 10.10.10.196, 10.10.10.197, 10.10.10.198, 10.10.10.199, 10.10.10.200, 10.10.10.201, 10.10.10.202, 10.10.10.203, 10.10.10.204, 10.10.10.205, 10.10.10.206, 10.10.10.207, 10.10.10.208, 10.10.10.209, 10.10.10.210, 10.10.10.211, 10.10.10.212, 10.10.10.213, 10.10.10.214, 10.10.10.215, 10.10.10.216, 10.10.10.217, 10.10.10.218, 10.10.10.219, 10.10.10.220, 10.10.10.221, 10.10.10.222, 10.10.10.223, 10.10.10.224, 10.10.10.225, 10.10.10.226, 10.10.10.227, 10.10.10.228, 10.10.10.229, 10.10.10.230, 10.10.10.231, 10.10.10.232, 10.10.10.233, 10.10.10.234, 10.10.10.235, 10.10.10.236, 10.10.10.237, 10.10.10.238, 10.10.10.239, 10.10.10.240, 10.10.10.241, 10.10.10.242, 10.10.10.243, 10.10.10.244, 10.10.10.245, 10.10.10.246, 10.10.10.247, 10.10.10.248, 10.10.10.249, 10.10.10.250, 10.10.10.251, 10.10.10.252, 10.10.10.253, 10.10.10.254, 10.10.10.255
5D10.10.10.0-10.10.11.010.10.10.0, 10.10.10.1, 10.10.10.2, 10.10.10.3, 10.10.10.4, 10.10.10.5, 10.10.10.6, 10.10.10.7, 10.10.10.8, 10.10.10.9, 10.10.10.10, 10.10.10.11, 10.10.10.12, 10.10.10.13, 10.10.10.14, 10.10.10.15, 10.10.10.16, 10.10.10.17, 10.10.10.18, 10.10.10.19, 10.10.10.20, 10.10.10.21, 10.10.10.22, 10.10.10.23, 10.10.10.24, 10.10.10.25, 10.10.10.26, 10.10.10.27, 10.10.10.28, 10.10.10.29, 10.10.10.30, 10.10.10.31, 10.10.10.32, 10.10.10.33, 10.10.10.34, 10.10.10.35, 10.10.10.36, 10.10.10.37, 10.10.10.38, 10.10.10.39, 10.10.10.40, 10.10.10.41, 10.10.10.42, 10.10.10.43, 10.10.10.44, 10.10.10.45, 10.10.10.46, 10.10.10.47, 10.10.10.48, 10.10.10.49, 10.10.10.50, 10.10.10.51, 10.10.10.52, 10.10.10.53, 10.10.10.54, 10.10.10.55, 10.10.10.56, 10.10.10.57, 10.10.10.58, 10.10.10.59, 10.10.10.60, 10.10.10.61, 10.10.10.62, 10.10.10.63, 10.10.10.64, 10.10.10.65, 10.10.10.66, 10.10.10.67, 10.10.10.68, 10.10.10.69, 10.10.10.70, 10.10.10.71, 10.10.10.72, 10.10.10.73, 10.10.10.74, 10.10.10.75, 10.10.10.76, 10.10.10.77, 10.10.10.78, 10.10.10.79, 10.10.10.80, 10.10.10.81, 10.10.10.82, 10.10.10.83, 10.10.10.84, 10.10.10.85, 10.10.10.86, 10.10.10.87, 10.10.10.88, 10.10.10.89, 10.10.10.90, 10.10.10.91, 10.10.10.92, 10.10.10.93, 10.10.10.94, 10.10.10.95, 10.10.10.96, 10.10.10.97, 10.10.10.98, 10.10.10.99, 10.10.10.100, 10.10.10.101, 10.10.10.102, 10.10.10.103, 10.10.10.104, 10.10.10.105, 10.10.10.106, 10.10.10.107, 10.10.10.108, 10.10.10.109, 10.10.10.110, 10.10.10.111, 10.10.10.112, 10.10.10.113, 10.10.10.114, 10.10.10.115, 10.10.10.116, 10.10.10.117, 10.10.10.118, 10.10.10.119, 10.10.10.120, 10.10.10.121, 10.10.10.122, 10.10.10.123, 10.10.10.124, 10.10.10.125, 10.10.10.126, 10.10.10.127, 10.10.10.128, 10.10.10.129, 10.10.10.130, 10.10.10.131, 10.10.10.132, 10.10.10.133, 10.10.10.134, 10.10.10.135, 10.10.10.136, 10.10.10.137, 10.10.10.138, 10.10.10.139, 10.10.10.140, 10.10.10.141, 10.10.10.142, 10.10.10.143, 10.10.10.144, 10.10.10.145, 10.10.10.146, 10.10.10.147, 10.10.10.148, 10.10.10.149, 10.10.10.150, 10.10.10.151, 10.10.10.152, 10.10.10.153, 10.10.10.154, 10.10.10.155, 10.10.10.156, 10.10.10.157, 10.10.10.158, 10.10.10.159, 10.10.10.160, 10.10.10.161, 10.10.10.162, 10.10.10.163, 10.10.10.164, 10.10.10.165, 10.10.10.166, 10.10.10.167, 10.10.10.168, 10.10.10.169, 10.10.10.170, 10.10.10.171, 10.10.10.172, 10.10.10.173, 10.10.10.174, 10.10.10.175, 10.10.10.176, 10.10.10.177, 10.10.10.178, 10.10.10.179, 10.10.10.180, 10.10.10.181, 10.10.10.182, 10.10.10.183, 10.10.10.184, 10.10.10.185, 10.10.10.186, 10.10.10.187, 10.10.10.188, 10.10.10.189, 10.10.10.190, 10.10.10.191, 10.10.10.192, 10.10.10.193, 10.10.10.194, 10.10.10.195, 10.10.10.196, 10.10.10.197, 10.10.10.198, 10.10.10.199, 10.10.10.200, 10.10.10.201, 10.10.10.202, 10.10.10.203, 10.10.10.204, 10.10.10.205, 10.10.10.206, 10.10.10.207, 10.10.10.208, 10.10.10.209, 10.10.10.210, 10.10.10.211, 10.10.10.212, 10.10.10.213, 10.10.10.214, 10.10.10.215, 10.10.10.216, 10.10.10.217, 10.10.10.218, 10.10.10.219, 10.10.10.220, 10.10.10.221, 10.10.10.222, 10.10.10.223, 10.10.10.224, 10.10.10.225, 10.10.10.226, 10.10.10.227, 10.10.10.228, 10.10.10.229, 10.10.10.230, 10.10.10.231, 10.10.10.232, 10.10.10.233, 10.10.10.234, 10.10.10.235, 10.10.10.236, 10.10.10.237, 10.10.10.238, 10.10.10.239, 10.10.10.240, 10.10.10.241, 10.10.10.242, 10.10.10.243, 10.10.10.244, 10.10.10.245, 10.10.10.246, 10.10.10.247, 10.10.10.248, 10.10.10.249, 10.10.10.250, 10.10.10.251, 10.10.10.252, 10.10.10.253, 10.10.10.254, 10.10.10.255, 10.10.11.0, 10.10.11.0
6E10.10.10.0-10.11.11.010.10.10.0, 10.10.10.1, 10.10.10.2, 10.10.10.3, 10.10.10.4, 10.10.10.5, 10.10.10.6, 10.10.10.7, 10.10.10.8, 10.10.10.9, 10.10.10.10, 10.10.10.11, 10.10.10.12, 10.10.10.13, 10.10.10.14, 10.10.10.15, 10.10.10.16, 10.10.10.17, 10.10.10.18, 10.10.10.19, 10.10.10.20, 10.10.10.21, 10.10.10.22, 10.10.10.23, 10.10.10.24, 10.10.10.25, 10.10.10.26, 10.10.10.27, 10.10.10.28, 10.10.10.29, 10.10.10.30, 10.10.10.31, 10.10.10.32, 10.10.10.33, 10.10.10.34, 10.10.10.35, 10.10.10.36, 10.10.10.37, 10.10.10.38, 10.10.10.39, 10.10.10.40, 10.10.10.41, 10.10.10.42, 10.10.10.43, 10.10.10.44, 10.10.10.45, 10.10.10.46, 10.10.10.47, 10.10.10.48, 10.10.10.49, 10.10.10.50, 10.10.10.51, 10.10.10.52, 10.10.10.53, 10.10.10.54, 10.10.10.55, 10.10.10.56, 10.10.10.57, 10.10.10.58, 10.10.10.59, 10.10.10.60, 10.10.10.61, 10.10.10.62, 10.10.10.63, 10.10.10.64, 10.10.10.65, 10.10.10.66, 10.10.10.67, 10.10.10.68, 10.10.10.69, 10.10.10.70, 10.10.10.71, 10.10.10.72, 10.10.10.73, 10.10.10.74, 10.10.10.75, 10.10.10.76, 10.10.10.77, 10.10.10.78, 10.10.10.79, 10.10.10.80, 10.10.10.81, 10.10.10.82, 10.10.10.83, 10.10.10.84, 10.10.10.85, 10.10.10.86, 10.10.10.87, 10.10.10.88, 10.10.10.89, 10.10.10.90, 10.10.10.91, 10.10.10.92, 10.10.10.93, 10.10.10.94, 10.10.10.95, 10.10.10.96, 10.10.10.97, 10.10.10.98, 10.10.10.99, 10.10.10.100, 10.10.10.101, 10.10.10.102, 10.10.10.103, 10.10.10.104, 10.10.10.105, 10.10.10.106, 10.10.10.107, 10.10.10.108, 10.10.10.109, 10.10.10.110, 10.10.10.111, 10.10.10.112, 10.10.10.113, 10.10.10.114, 10.10.10.115, 10.10.10.116, 10.10.10.117, 10.10.10.118, 10.10.10.119, 10.10.10.120, 10.10.10.121, 10.10.10.122, 10.10.10.123, 10.10.10.124, 10.10.10.125, 10.10.10.126, 10.10.10.127, 10.10.10.128, 10.10.10.129, 10.10.10.130, 10.10.10.131, 10.10.10.132, 10.10.10.133, 10.10.10.134, 10.10.10.135, 10.10.10.136, 10.10.10.137, 10.10.10.138, 10.10.10.139, 10.10.10.140, 10.10.10.141, 10.10.10.142, 10.10.10.143, 10.10.10.144, 10.10.10.145, 10.10.10.146, 10.10.10.147, 10.10.10.148, 10.10.10.149, 10.10.10.150, 10.10.10.151, 10.10.10.152, 10.10.10.153, 10.10.10.154, 10.10.10.155, 10.10.10.156, 10.10.10.157, 10.10.10.158, 10.10.10.159, 10.10.10.160, 10.10.10.161, 10.10.10.162, 10.10.10.163, 10.10.10.164, 10.10.10.165, 10.10.10.166, 10.10.10.167, 10.10.10.168, 10.10.10.169, 10.10.10.170, 10.10.10.171, 10.10.10.172, 10.10.10.173, 10.10.10.174, 10.10.10.175, 10.10.10.176, 10.10.10.177, 10.10.10.178, 10.10.10.179, 10.10.10.180, 10.10.10.181, 10.10.10.182, 10.10.10.183, 10.10.10.184, 10.10.10.185, 10.10.10.186, 10.10.10.187, 10.10.10.188, 10.10.10.189, 10.10.10.190, 10.10.10.191, 10.10.10.192, 10.10.10.193, 10.10.10.194, 10.10.10.195, 10.10.10.196, 10.10.10.197, 10.10.10.198, 10.10.10.199, 10.10.10.200, 10.10.10.201, 10.10.10.202, 10.10.10.203, 10.10.10.204, 10.10.10.205, 10.10.10.206, 10.10.10.207, 10.10.10.208, 10.10.10.209, 10.10.10.210, 10.10.10.211, 10.10.10.212, 10.10.10.213, 10.10.10.214, 10.10.10.215, 10.10.10.216, 10.10.10.217, 10.10.10.218, 10.10.10.219, 10.10.10.220, 10.10.10.221, 10.10.10.222, 10.10.10.223, 10.10.10.224, 10.10.10.225, 10.10.10.226, 10.10.10.227, 10.10.10.228, 10.10.10.229, 10.10.10.230, 10.10.10.231, 10.10.10.232, 10.10.10.233, 10.10.10.234, 10.10.10.235, 10.10.10.236, 10.10.10.237, 10.10.10.238, 10.10.10.239, 10.10.10.240, 10.10.10.241, 10.10.10.242, 10.10.10.243, 10.10.10.244, 10.10.10.245, 10.10.10.246, 10.10.10.247, 10.10.10.248, 10.10.10.249, 10.10.10.250, 10.10.10.251, 10.10.10.252, 10.10.10.253, 10.10.10.254, 10.10.10.255, 10.10.11.0, 10.10.11.0, 10.10.11.1, 10.10.11.2, 10.10.11.3, 10.10.11.4, 10.10.11.5, 10.10.11.6, 10.10.11.7, 10.10.11.8, 10.10.11.9, 10.10.11.10, 10.10.11.11, 10.10.11.12, 10.10.11.13, 10.10.11.14, 10.10.11.15, 10.10.11.16, 10.10.11.17, 10.10.11.18, 10.10.11.19, 10.10.11.20, 10.10.11.21, 10.10.11.22, 10.10.11.23, 10.10.11.24, 10.10.11.25, 10.10.11.26, 10.10.11.27, 10.10.11.28, 10.10.11.29, 10.10.11.30, 10.10.11.31, 10.10.11.32, 10.10.11.33, 10.10.11.34, 10.10.11.35, 10.10.11.36, 10.10.11.37, 10.10.11.38, 10.10.11.39, 10.10.11.40, 10.10.11.41, 10.10.11.42, 10.10.11.43, 10.10.11.44, 10.10.11.45, 10.10.11.46, 10.10.11.47, 10.10.11.48, 10.10.11.49, 10.10.11.50, 10.10.11.51, 10.10.11.52, 10.10.11.53, 10.10.11.54, 10.10.11.55, 10.10.11.56, 10.10.11.57, 10.10.11.58, 10.10.11.59, 10.10.11.60, 10.10.11.61, 10.10.11.62, 10.10.11.63, 10.10.11.64, 10.10.11.65, 10.10.11.66, 10.10.11.67, 10.10.11.68, 10.10.11.69, 10.10.11.70, 10.10.11.71, 10.10.11.72, 10.10.11.73, 10.10.11.74, 10.10.11.75, 10.10.11.76, 10.10.11.77, 10.10.11.78, 10.10.11.79, 10.10.11.80, 10.10.11.81, 10.10.11.82, 10.10.11.83, 10.10.11.84, 10.10.11.85, 10.10.11.86, 10.10.11.87, 10.10.11.88, 10.10.11.89, 10.10.11.90, 10.10.11.91, 10.10.11.92, 10.10.11.93, 10.10.11.94, 10.10.11.95, 10.10.11.96, 10.10.11.97, 10.10.11.98, 10.10.11.99, 10.10.11.100, 10.10.11.101, 10.10.11.102, 10.10.11.103, 10.10.11.104, 10.10.11.105, 10.10.11.106, 10.10.11.107, 10.10.11.108, 10.10.11.109, 10.10.11.110, 10.10.11.111, 10.10.11.112, 10.10.11.113, 10.10.11.114, 10.10.11.115, 10.10.11.116, 10.10.11.117, 10.10.11.118, 10.10.11.119, 10.10.11.120, 10.10.11.121, 10.10.11.122, 10.10.11.123, 10.10.11.124, 10.10.11.125, 10.10.11.126, 10.10.11.127, 10.10.11.128, 10.10.11.129, 10.10.11.130, 10.10.11.131, 10.10.11.132, 10.10.11.133, 10.10.11.134, 10.10.11.135, 10.10.11.136, 10.10.11.137, 10.10.11.138, 10.10.11.139, 10.10.11.140, 10.10.11.141, 10.10.11.142, 10.10.11.143, 10.10.11.144, 10.10.11.145, 10.10.11.146, 10.10.11.147, 10.10.11.148, 10.10.11.149, 10.10.11.150, 10.10.11.151, 10.10.11.152, 10.10.11.153, 10.10.11.154, 10.10.11.155, 10.10.11.156, 10.10.11.157, 10.10.11.158, 10.10.11.159, 10.10.11.160, 10.10.11.161, 10.10.11.162, 10.10.11.163, 10.10.11.164, 10.10.11.165, 10.10.11.166, 10.10.11.167, 10.10.11.168, 10.10.11.169, 10.10.11.170, 10.10.11.171, 10.10.11.172, 10.10.11.173, 10.10.11.174, 10.10.11.175, 10.10.11.176, 10.10.11.177, 10.10.11.178, 10.10.11.179, 10.10.11.180, 10.10.11.181, 10.10.11.182, 10.10.11.183, 10.10.11.184, 10.10.11.185, 10.10.11.186, 10.10.11.187, 10.10.11.188, 10.10.11.189, 10.10.11.190, 10.10.11.191, 10.10.11.192, 10.10.11.193, 10.10.11.194, 10.10.11.195, 10.10.11.196, 10.10.11.197, 10.10.11.198, 10.10.11.199, 10.10.11.200, 10.10.11.201, 10.10.11.202, 10.10.11.203, 10.10.11.204, 10.10.11.205, 10.10.11.206, 10.10.11.207, 10.10.11.208, 10.10.11.209, 10.10.11.210, 10.10.11.211, 10.10.11.212, 10.10.11.213, 10.10.11.214, 10.10.11.215, 10.10.11.216, 10.10.11.217, 10.10.11.218, 10.10.11.219, 10.10.11.220, 10.10.11.221, 10.10.11.222, 10.10.11.223, 10.10.11.224, 10.10.11.225, 10.10.11.226, 10.10.11.227, 10.10.11.228, 10.10.11.229, 10.10.11.230, 10.10.11.231, 10.10.11.232, 10.10.11.233, 10.10.11.234, 10.10.11.235, 10.10.11.236, 10.10.11.237, 10.10.11.238, 10.10.11.239, 10.10.11.240, 10.10.11.241, 10.10.11.242, 10.10.11.243, 10.10.11.244, 10.10.11.245, 10.10.11.246, 10.10.11.247, 10.10.11.248, 10.10.11.249, 10.10.11.250, 10.10.11.251, 10.10.11.252, 10.10.11.253, 10.10.11.254, 10.10.11.255, 10.10.12.0, 10.10.12.0, 10.10.12.1, 10.10.12.2, 10.10.12.3, 10.10.12.4, 10.10.12.5, 10.10.12.6, 10.10.12.7, 10.10.12.8, 10.10.12.9, 10.10.12.10, 10.10.12.11, 10.10.12.12, 10.10.12.13, 10.10.12.14, 10.10.12.15, 10.10.12.16, 10.10.12.17, 10.10.12.18, 10.10.12.19, 10.10.12.20, 10.10.12.21, 10.10.12.22, 10.10.12.23, 10.10.12.24, 10.10.12.25, 10.10.12.26, 10.10.12.27, 10.10.12.28, 10.10.12.29, 10.10.12.30, 10.10.12.31, 10.10.12.32, 10.10.12.33, 10.10.12.34, 10.10.12.35, 10.10.12.36, 10.10.12.37, 10.10.12.38, 10.10.12.39, 10.10.12.40, 10.10.12.41, 10.10.12.42, 10.10.12.43, 10.10.12.44, 10.10.12.45, 10.10.12.46, 10.10.12.47, 10.10.12.48, 10.10.12.49, 10.10.12.50, 10.10.12.51, 10.10.12.52, 10.10.12.53, 10.10.12.54, 10.10.12.55, 10.10.12.56, 10.10.12.57, 10.10.12.58, 10.10.12.59, 10.10.12.60, 10.10.12.61, 10.10.12.62, 10.10.12.63, 10.10.12.64, 10.10.12.65, 10.10.12.66, 10.10.12.67, 10.10.12.68, 10.10.12.69, 10.10.12.70, 10.10.12.71, 10.10.12.72, 10.10.12.73, 10.10.12.74, 10.10.12.75, 10.10.12.76, 10.10.12.77, 10.10.12.78, 10.10.12.79, 10.10.12.80, 10.10.12.81, 10.10.12.82, 10.10.12.83, 10.10.12.84, 10.10.12.85, 10.10.12.86, 10.10.12.87, 10.10.12.88, 10.10.12.89, 10.10.12.90, 10.10.12.91, 10.10.12.92, 10.10.12.93, 10.10.12.94, 10.10.12.95, 10.10.12.96
7F10.15.1.0-10.15.1.5,10.15.2.3-10.15.2.510.15.1.0, 10.15.1.1, 10.15.1.2, 10.15.1.3, 10.15.1.4, 10.15.1.5, 10.15.2.3, 10.15.2.4, 10.15.2.5
8G10.25.25.127-10.25.26.310.25.25.127, 10.25.25.128, 10.25.25.129, 10.25.25.130, 10.25.25.131, 10.25.25.132, 10.25.25.133, 10.25.25.134, 10.25.25.135, 10.25.25.136, 10.25.25.137, 10.25.25.138, 10.25.25.139, 10.25.25.140, 10.25.25.141, 10.25.25.142, 10.25.25.143, 10.25.25.144, 10.25.25.145, 10.25.25.146, 10.25.25.147, 10.25.25.148, 10.25.25.149, 10.25.25.150, 10.25.25.151, 10.25.25.152, 10.25.25.153, 10.25.25.154, 10.25.25.155, 10.25.25.156, 10.25.25.157, 10.25.25.158, 10.25.25.159, 10.25.25.160, 10.25.25.161, 10.25.25.162, 10.25.25.163, 10.25.25.164, 10.25.25.165, 10.25.25.166, 10.25.25.167, 10.25.25.168, 10.25.25.169, 10.25.25.170, 10.25.25.171, 10.25.25.172, 10.25.25.173, 10.25.25.174, 10.25.25.175, 10.25.25.176, 10.25.25.177, 10.25.25.178, 10.25.25.179, 10.25.25.180, 10.25.25.181, 10.25.25.182, 10.25.25.183, 10.25.25.184, 10.25.25.185, 10.25.25.186, 10.25.25.187, 10.25.25.188, 10.25.25.189, 10.25.25.190, 10.25.25.191, 10.25.25.192, 10.25.25.193, 10.25.25.194, 10.25.25.195, 10.25.25.196, 10.25.25.197, 10.25.25.198, 10.25.25.199, 10.25.25.200, 10.25.25.201, 10.25.25.202, 10.25.25.203, 10.25.25.204, 10.25.25.205, 10.25.25.206, 10.25.25.207, 10.25.25.208, 10.25.25.209, 10.25.25.210, 10.25.25.211, 10.25.25.212, 10.25.25.213, 10.25.25.214, 10.25.25.215, 10.25.25.216, 10.25.25.217, 10.25.25.218, 10.25.25.219, 10.25.25.220, 10.25.25.221, 10.25.25.222, 10.25.25.223, 10.25.25.224, 10.25.25.225, 10.25.25.226, 10.25.25.227, 10.25.25.228, 10.25.25.229, 10.25.25.230, 10.25.25.231, 10.25.25.232, 10.25.25.233, 10.25.25.234, 10.25.25.235, 10.25.25.236, 10.25.25.237, 10.25.25.238, 10.25.25.239, 10.25.25.240, 10.25.25.241, 10.25.25.242, 10.25.25.243, 10.25.25.244, 10.25.25.245, 10.25.25.246, 10.25.25.247, 10.25.25.248, 10.25.25.249, 10.25.25.250, 10.25.25.251, 10.25.25.252, 10.25.25.253, 10.25.25.254, 10.25.25.255, 10.25.26.0, 10.25.26.0, 10.25.26.1, 10.25.26.2, 10.25.26.3
Sheet2


VBA Code:
Sub ExpandIP()
Application.ScreenUpdating = False
Dim r As Range:         Set r = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim SP() As String
Dim IP() As String

With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(AR)
        If InStr(AR(i, 1), "-") > 0 Then
            SP = Split(AR(i, 1), ",")
            For j = 0 To UBound(SP)
                IP = Split(SP(j), "-")
                .Add CountIP(IP(0), IP(1)), 1
            Next j
        Else
            .Add AR(i, 1), 1
        End If
        Cells(i + 1, 3).Value = Join(.Keys, ", ")
        .RemoveAll
    Next i
End With

Application.ScreenUpdating = True
End Sub

Function CountIP(SIP As String, EIP As String)
Dim IP1() As String:    IP1 = Split(SIP, ".")
Dim IP2() As String:    IP2 = Split(EIP, ".")
Dim b As Boolean:       b = True

With CreateObject("System.Collections.ArrayList")
    Do Until Join(IP1, ".") = Join(IP2, ".")
        b = True
        For i = 1 To 3
            If IP1(i) = 256 Then
                IP1(i - 1) = IP1(i - 1) + 1
                IP1(i) = 0
                b = False
            End If
        Next i
        .Add Join(IP1, ".")
        If b Then IP1(3) = IP1(3) + 1
    Loop
    .Add Join(IP1, ".")
    CountIP = Join(.toArray, ", ")
End With
End Function
 
Upvote 0
Again,
thanks so much. Is there a limit of the numbers for values you can insert into column c (Results from VBA Script) ? (Perhaps array size limitation)
I noticed on the output on company E, the values stopped at (.......10.10.19.106, 10.10)

if this happens can that row be flagged somehow so I can split up the ranges and run it again? Perhaps put the word "Error" instead of the actual output

Thank you
 
Upvote 0
Yes, there is a character limit for each cell of 32,767 characters long.

This update will output an error if the result exceeds that length.

VBA Code:
Sub ExpandIP()
Application.ScreenUpdating = False
Dim r As Range:         Set r = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim SP() As String
Dim IP() As String
Dim rLen As Long

With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(AR)
        If InStr(AR(i, 1), "-") > 0 Then
            SP = Split(AR(i, 1), ",")
            For j = 0 To UBound(SP)
                IP = Split(SP(j), "-")
                .Add CountIP(IP(0), IP(1)), 1
            Next j
        Else
            .Add AR(i, 1), 1
        End If
        rLen = Len(Join(.Keys, ", "))
        If rLen > 32767 Then
            Cells(i + 1, 3).Value = "Range too large. Results " & Format(rLen, "#,##0") & " characters long."
        Else
            Cells(i + 1, 3).Value = Join(.Keys, ",")
        End If
        .RemoveAll
    Next i
End With

Application.ScreenUpdating = True
End Sub

Function CountIP(SIP As String, EIP As String)
Dim IP1() As String:    IP1 = Split(SIP, ".")
Dim IP2() As String:    IP2 = Split(EIP, ".")
Dim b As Boolean:       b = True

With CreateObject("System.Collections.ArrayList")
    Do Until Join(IP1, ".") = Join(IP2, ".")
        b = True
        For i = 1 To 3
            If IP1(i) = 256 Then
                IP1(i - 1) = IP1(i - 1) + 1
                IP1(i) = 0
                b = False
            End If
        Next i
        .Add Join(IP1, ".")
        If b Then IP1(3) = IP1(3) + 1
    Loop
    .Add Join(IP1, ".")
    CountIP = Join(.toArray, ",")
End With
End Function
 
Upvote 0
Solution
Thanks so much. I'll learn much from stepping thru you code. :)
 
Upvote 0
No problem. Glad to help. Let me know if you have any questions.
 
Upvote 0
Good Morning

I found a issue using the following data

CompanyIP Range
A10.10.10.0
B10.10.10.0-10.10.10.2,192.168.1.0

Looks like the IP Ranges can be
10.10.10.0
10.10.10.0-10.10.10.2
10.10.10.0-10.10.10.2,192.168.1.0
10.10.10.0-10.10.10.2,192.168.1.0,192.168.1.5
10.10.10.0-10.10.10.2,192.168.1.0,192.168.1.5,200.1.1.0-200.1.1.3
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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