Using an array in a for loop

ReggaeYT

New Member
Joined
Jul 24, 2014
Messages
16
Hey all,

Looking for some help with arrays, and using them as a range in my for loop.

Currently here's what I have (that's relevant)

Dim planRows(14) As Integer
Dim z As Variant

<many lines that dictate whats in the array>

For c = a To b
For z = LBound(planRows) To UBound(planRows)
Sheets("Delta Tracking Log").Cells(c, 3) = Sheets(sheetName).Cells(z, 8)
Sheets("Delta Tracking Log").Cells(c, 4) = Sheets(sheetName).Cells(z, 12)
Sheets("Delta Tracking Log").Cells(c, 5) = Sheets(sheetName).Cells(z, 16)
Next z
Next c

--------------------------------------------------------------------

I'm getting a 400 error every time, and it all stems from the line For z = LBound(planRows) To UBound(planRows)
I know this because I tried a "For Each" in its place, just to test it out, and it over-wrote the 3 lines repeatedly with just the last value in the array.

Can anyone help? Can provide the full VBA if necessary.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Dim z As Long and see if you still get the error. It is assumed that planRows array has already been loaded.
 
Upvote 0
Unfortunately no, it still doesn't work with that adjustment. Here is the full VBA code so far:

Sub RunIt()
Dim i, a, b, c, d As Integer
Dim sheetName As Variant
Dim planRows(14) As Integer
Dim z As Long

planRows(0) = 19
planRows(1) = 20
planRows(2) = 21
planRows(3) = 22
planRows(4) = 34
planRows(5) = 35
planRows(6) = 36
planRows(7) = 37
planRows(8) = 38
planRows(9) = 39
planRows(10) = 51
planRows(11) = 52
planRows(12) = 53
planRows(13) = 54
planRows(14) = 55


sheetName = InputBox("What is the name of the tab who's data you want to process?")

Sheets("Delta Tracking Log").Cells(1, 21) = sheetName

For i = 2 To 5000
If IsEmpty(Sheets("Delta Tracking Log").Cells(i, 1)) = True Then
a = i
b = a + 26
Exit For
Else
Sheets("Delta Tracking Log").Cells(1, 21) = sheetName
End If
Next i

For c = a To b
For z = LBound(planRows) To UBound(planRows)
Sheets("Delta Tracking Log").Cells(c, 3) = Sheets(sheetName).Cells(z, 8)
Sheets("Delta Tracking Log").Cells(c, 4) = Sheets(sheetName).Cells(z, 12)
Sheets("Delta Tracking Log").Cells(c, 5) = Sheets(sheetName).Cells(z, 16)
Sheets("Delta Tracking Log").Cells(c, 6) = Sheets(sheetName).Cells(z, 20)
Sheets("Delta Tracking Log").Cells(c, 7) = Sheets(sheetName).Cells(z, 21)
Sheets("Delta Tracking Log").Cells(c, 8) = Sheets(sheetName).Cells(z, 25)
Sheets("Delta Tracking Log").Cells(c, 9) = Sheets(sheetName).Cells(z, 29)
Sheets("Delta Tracking Log").Cells(c, 10) = Sheets(sheetName).Cells(z, 33)
Sheets("Delta Tracking Log").Cells(c, 11) = Sheets(sheetName).Cells(z, 37)
Sheets("Delta Tracking Log").Cells(c, 12) = Sheets(sheetName).Cells(z, 38)
Sheets("Delta Tracking Log").Cells(c, 13) = Sheets(sheetName).Cells(z, 42)
Sheets("Delta Tracking Log").Cells(c, 14) = Sheets(sheetName).Cells(z, 46)
Sheets("Delta Tracking Log").Cells(c, 15) = Sheets(sheetName).Cells(z, 50)
Sheets("Delta Tracking Log").Cells(c, 16) = Sheets(sheetName).Cells(z, 54)
Sheets("Delta Tracking Log").Cells(c, 17) = Sheets(sheetName).Cells(z, 56)
Next z
Next c


MsgBox "All set, I hope"
End Sub
 
Upvote 0
Fixed. My whole process was wrong. Corrected it, and that type change above really helped. Thanks!
 
Upvote 0
Code:
For z = LBound(planRows) To UBound(planRows)
There is nothing wrong with this statement...

Gut there appears to be a problem with this structure:
Code:
For i = 2 To 5000
    If IsEmpty(Sheets("Delta Tracking Log").Cells(i, 1)) = True Then
        a = i
        b = a + 26
        Exit For
    Else
        Sheets("Delta Tracking Log").Cells(1, 21) = sheetName
    End If
 Next i
 For c = a To b
    For z = LBound(planRows) To UBound(planRows)

If Sheets("Delta Tracking Log").Cell(i, 1) does have data then the code enters sheetName in cells(1, 21) and it will do that 4999 times. Then it will go to the 'For c = a To b line of code and throw an error because a and b have not been initialized. I don't understand what you are trying to do in this For i Loop but to avoid the error, you would need some kind of If statement with criterea that would bybass the double loops (For c and For z) if varibles a and b are not initialized. Maybe this
Code:
If Not IaEmpty(a) Then
 'your double loop here
End If

Why are you looping 4999 times,? Maybe that can be reduced if you explaing your objective.
 
Upvote 0

Forum statistics

Threads
1,213,562
Messages
6,114,322
Members
448,564
Latest member
ED38

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