Nested For Next Loop not working

Dman333

Board Regular
Joined
Sep 30, 2016
Messages
52
Office Version
  1. 365
Platform
  1. Windows
Hello everyone - I'm trying to get a nested for next loop to work. The inside loop one pulls some data based on dates, cost codes and puts it into a sheet. But I need to do this with up to 4 BusUnits at a time. The form that drives this and the inside loop work, its just when I tried to get an outside loop to go through 4 BusUnits, and particularly the attempt to use the variant in the If Then formula. I feel like I'm pretty close and any help is greatly appreciated.

Thanks



Dim arrbu() As Variant
Dim arrcc() As Variant
Dim bu As Variant
Dim BusUnit As Variant
Dim Ccode As Variant

arrbu = Array("Busunit1", "Busunit2", "Busunit3", "Busunit4")
arrcc = Array("ccode1", "ccode2", "ccode3", "ccode4")


For Each bu In arrbu

BusUnit = arrbu
Ccode = arrcc

For X = 5 To 10000 'wslr
If TExportws.Cells(X, 21) >= ws.Range("StartDate") And _
TExportws.Cells(X, 21) <= ws.Range("EndDate") And _
TExportws.Cells(X, 6) = Ccode And _
TExportws.Cells(X, 25) = BusUnit Then

ws.Cells(Row, 17) = Val(TExportws.Cells(X, 25)) 'Bus Unit Number
ws.Cells(Row, 18) = TExportws.Cells(X, 6) 'Cost Code
ws.Cells(Row, 19) = TExportws.Cells(X, 7) 'Description
ws.Cells(Row, 20) = TExportws.Cells(X, 8) 'Old Value
ws.Cells(Row, 21) = TExportws.Cells(X, 9) 'New Value
ws.Cells(Row, 22) = TExportws.Cells(X, 10) 'Previous Budget - Line Item
ws.Cells(Row, 23) = TExportws.Cells(X, 11) 'Revised Budget - Line Item
ws.Cells(Row, 24) = TExportws.Cells(X, 12) 'Increase / (Decrease) - Line Item Budget
ws.Cells(Row, 25) = TExportws.Cells(X, 14) 'Previous Budget - Cost Code
ws.Cells(Row, 26) = TExportws.Cells(X, 15) 'Revised Budget - Cost Code
ws.Cells(Row, 27) = TExportws.Cells(X, 16) 'Increase / (Decrease) - Cost Code Budget
ws.Cells(Row, 28) = TExportws.Cells(X, 13) 'Notes

ws.Cells(Row, 17).NumberFormat = "General"
ws.Cells(Row, 18).NumberFormat = "General"
ws.Cells(Row, 20).NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
ws.Cells(Row, 21).NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
ws.Cells(Row, 22).NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
ws.Cells(Row, 23).NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
ws.Cells(Row, 24).NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
ws.Cells(Row, 25).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
ws.Cells(Row, 26).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
ws.Cells(Row, 27).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"

Row = Row + 1

End If
Next X
Next bu
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
When posting code, remember to post it inside code tags - by clicking on <vba/> or <rich/> which makes it easier to read (look at the code below and compare it to post#1)

These 2 lines in your code look wrong to me - both arrbu and arrcc are arrays of values - you are wanting to use the individual values
BusUnit = arrbu
Ccode = arrcc


It is not clear to me exactly how you want to loop, but this may help you get there
- it illustrates how to get the individual values
Run macro below and look at the Immediate Window to see what is produced
- display Immediate Window in VBA editor with {CTRL} g

VBA Code:
Sub BusUnits()
    Dim arrbu() As Variant
    Dim arrcc() As Variant
    Dim bu As Variant
    Dim BusUnit As Variant
    Dim Ccode As Variant
   
    arrbu = Array("Busunit1", "Busunit2", "Busunit3", "Busunit4")
    arrcc = Array("ccode1", "ccode2", "ccode3", "ccode4")

    For Each bu In arrbu
        For Each Ccode In arrcc
            Debug.Print bu, Ccode
        Next Ccode
    Next bu
End Sub
 
Upvote 0
Thank you so much! I'm pretty new to VBA and this helped get me where I needed to go. I changed my approach and used a range instead of the array and that got the loop to work the way
I needed.
Very much appreciated!
Cheers
 
Upvote 0
Solution

Forum statistics

Threads
1,214,875
Messages
6,122,042
Members
449,063
Latest member
ak94

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