Max number for arrays

Danny54

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

I've been working on the following macro trying to get all the values in a range for several weeks now. Im at a loss and have had several people help that have me to this point.
the code keeps erroring out and Im not certain of what to do next. Explanation of the macro: it takes the ip values and list them on sheet2 after expanding them by count
ie 10.10.10.1-10.10.10.3 would list 10.10.10.1 and 10.10.10.2 and 10.10.10.3 in column b of sheet2. if we have the values 10.10.10.1-10.10.11.1 would list all the values beginning at
10.10.10.1 thru 10.10.11.1

The code stops on the line show " N = N + 1" below


Any help would be greatly appreciated.

Thanks

xls input
TitleIPs
Car10.218.68.3-11.218.68.3

Macro

Sub Expandit()
Dim W, S$(), N&, K&, V, L$(), R$(), A%, T$(3), B%, C%, D%
W = [Sheet1!A1].CurrentRegion.Value2
ReDim S(1 To Rows.Count, 1): S(1, 0) = W(1, 1): S(1, 1) = W(1, 2) ' get col1 and col2 into the array s
N = 1
For K = 2 To UBound(W)
For Each V In Split(W(K, 2), ",")
L = Split(V, "-")
If UBound(L) = 1 Then
R = Split(L(1), ".")
L = Split(L(0), ".")
If UBound(L) = 3 And UBound(R) = 3 Then
For A = L(0) To R(0)
T(0) = A
For B = -L(1) * (A = L(0) * 1) To IIf(A < R(0) * 1, 255, R(1))
T(1) = B
For C = -L(2) * (B = L(1) * 1) To IIf(B < R(1) * 1, 255, R(2))
T(2) = C
For D = -L(3) * (C = L(2) * 1) To IIf(C < R(2) * 1, 255, R(3))
T(3) = D
N = N + 1 '<-Error is happening here
S(N, 0) = W(K, 1)
S(N, 1) = Join(T, ".")
Next D, C, B, A
End If
Else
N = N + 1
S(N, 0) = W(K, 1)
S(N, 1) = V
End If
Next V, K
[Sheet2!A1:B1].Resize(N).Value2 = S

MsgBox "Completed"

End Sub

VBA Code:
Sub Expandit()
    Dim W, S$(), N&, K&, V, L$(), R$(), A%, T$(3), B%, C%, D%
        W = [Sheet1!A1].CurrentRegion.Value2
        ReDim S(1 To Rows.Count, 1):  S(1, 0) = W(1, 1):  S(1, 1) = W(1, 2)  ' get col1 and col2 into the array s
        N = 1
    For K = 2 To UBound(W)
    For Each V In Split(W(K, 2), ",")
            L = Split(V, "-")
        If UBound(L) = 1 Then
                R = Split(L(1), ".")
                L = Split(L(0), ".")
            If UBound(L) = 3 And UBound(R) = 3 Then
                For A = L(0) To R(0)
                    T(0) = A
                For B = -L(1) * (A = L(0) * 1) To IIf(A < R(0) * 1, 255, R(1))
                    T(1) = B
                For C = -L(2) * (B = L(1) * 1) To IIf(B < R(1) * 1, 255, R(2))
                    T(2) = C
                For D = -L(3) * (C = L(2) * 1) To IIf(C < R(2) * 1, 255, R(3))
                    T(3) = D
                    N = N + 1                                           '<-Error is happening here
                    S(N, 0) = W(K, 1)
                    S(N, 1) = Join(T, ".")
                Next D, C, B, A
            End If
        Else
            N = N + 1
            S(N, 0) = W(K, 1)
            S(N, 1) = V
        End If
    Next V, K
        [Sheet2!A1:B1].Resize(N).Value2 = S
  
    MsgBox "Completed"
      
End Sub
 
Last edited by a moderator:
Thanks for you help

Would there be a simple way to just write a dos file instead of putting it to a excel file?
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Should I probably rethink a new solution and abandon this code?

The original objective was to take a range of IP's, by line - expand them in order to create a listing that I could search to see if one exists and if so, get the Title
I have not worked right through this idea but might something like this be feasible?

VBA Code:
Sub Make_Ranges()
  Dim a As Variant, b As Variant, splt_1 As Variant, splt_2 As Variant, splt_3 As Variant, itm As Variant
  Dim i As Long, j As Long, K As Long
  Dim s As String
  
  a = Sheets("Sheet1").Range("A1").CurrentRegion.Value2
  ReDim b(1 To Rows.Count, 1 To 3)
  K = 1
  For i = 2 To UBound(a)
    splt_1 = Split(Replace(a(i, 2), " ", ""), ",")
    For Each itm In splt_1
      splt_2 = Split(itm & "-" & itm, "-", 3)
      K = K + 1
      b(K, 1) = a(i, 1)
      
      s = vbNullString
      splt_3 = Split(splt_2(0), ".")
      For j = 0 To 3
        s = s & Format(splt_3(j), "000")
      Next j
      b(K, 2) = s
      
      s = vbNullString
      splt_3 = Split(splt_2(1), ".")
      For j = 0 To 3
        s = s & Format(splt_3(j), "000")
      Next j
      b(K, 3) = s
    Next itm
  Next i
  With Sheets("Sheet2").Range("A1:C1").Resize(K)
    .NumberFormat = "000000000000"
    .Value2 = b
    .Rows(1).Value = Array("Title", "IP Start", "IP End")
    .Columns.AutoFit
  End With
End Sub

This just breaks up the data into individual IP ranges rather than individual IP values, so it would not use nearly as many rows. For example, this sample data in Sheet1

Danny54.xlsm
AB
1TitleIPs
2Car10.218.68.3-10.218.68.4
3Boat192.218.68.3-192.218.69.3, 12.218.68.3-12.218.68.7,10.4.5.5
4Other10.10.10.1, 10.10.10.3-10.10.25.3, 192.168.1.1-192.168.3.3, 192.168.20.1-192.168.20.25
Sheet1


becomes this in Sheet2

Danny54.xlsm
ABC
1TitleIP StartIP End
2Car010218068003010218068004
3Boat192218068003192218069003
4Boat012218068003012218068007
5Boat010004005005010004005005
6Other010010010001010010010001
7Other010010010003010010025003
8Other192168001001192168003003
9Other192168020001192168020025
Sheet2


If you then take your IP address of interest, expand it to full 12 digits like these and see which row (if any) has column B <= your IP of interest and column C >= your IP of interest to determine the relevant title?
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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