For Loop keep resetting itself after 58 iterations

bluejelly

New Member
Joined
May 31, 2017
Messages
4
Hi

I am having a problem i have never seen before and cannot work out why. I've searched for an answer but i've had no luck.

I have a table of data 57 columns wide. Columns 1 to 8 contain information such as name and date etc. Columns 9 to 57 show "production" numbers; each column represents a 30 minute period during the day. I'm trying to convert this table into a Tableau friendly format: i.e. Name, Date, Time and Production on all one row (one row per time per day per name)

I wrote the code below do this but when the For Loop gets to i=58, i seems to reset itself. If I have i in the watch window and a break point at "Next i", i get to 58 and when i resume the value of i in the watch window goes from 58 to " < Out of context> <out of="" context="">"<out of="" context="">. The code does not resume until i hit F5 again at which point i returns to 1.

Im pretty new to VBA so i'm hoping its an obvious newb mistake. Any help is appreciated!

Code:
Sub SmartestTableau()
Dim SourceArray As Variant
Dim OutputArray As Variant
SourceArray = Sheets("merged").UsedRange.Value

ReDim OutputArray(6, 1)

OutputArray(1, 1) = "Name"
OutputArray(2, 1) = ""
OutputArray(3, 1) = "Date"
OutputArray(4, 1) = "Time"
OutputArray(5, 1) = "Production"
OutputArray(6, 1) = "Time"

For i = 1 To UBound(SourceArray, 2)

    If SourceArray(i, 1) = "Yes" Then
    
        For j = 9 To 57

            ReDim Preserve OutputArray(6, UBound(OutputArray, 2) + 1)
            
            OutputArray(1, UBound(OutputArray, 2)) = SourceArray(i, 2)
            OutputArray(2, UBound(OutputArray, 2)) = ""
            OutputArray(3, UBound(OutputArray, 2)) = SourceArray(i, 8)
            OutputArray(4, UBound(OutputArray, 2)) = SourceArray(8, j)
            OutputArray(5, UBound(OutputArray, 2)) = SourceArray(i, j)
            OutputArray(6, UBound(OutputArray, 2)) = SourceArray(8, j)
            
        Next j

    End If


Next i
</out></out>
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Where is i declared?

After you've run the code through are you getting the correct results?
 
Upvote 0
Do I need to declare i? Usually my For Loops work without the declaration.

The UBound(SourceArray, 2) is over 10,000. As the code is only getting up to 56 I'm missing a huge amount of data. The code eventually continues (the loop resets to 1 several times) but I have tonnes worked out what causes the code to continue past the loop
 
Upvote 0
I don't understand how UBound(SourceArray, 2) is over 10,000 when you stated your table is 57 columns which would be what SourceArray, 2 is referring to.

Do you have 10,000 columns of data and not the 57 stated?

If you have 10,000 plus rows and want to loop based on that number use 1 instead of 2.
 
Last edited:
Upvote 0
Solution
If you want to loop through all the rows in the data the first/outer loop should look like this.
Code:
For i = 1 To UBound(SourceArray)

Within that loop I would expect to see another loop for the columns which would add a new row to the output row for every iteration.
Code:
  For j = 9 To 57


Can you explain further the late out of the source data?

Perhaps post a few rows with dummy data.
 
Upvote 0
Thanks! this is what is was! (i.e. me getting confused between array dimensions)

Thanks for spending time looking at this for me.

I don't understand how UBound(SourceArray, 2) is over 10,000 when you stated your table is 57 columns which would be what SourceArray, 2 is referring to.

Do you have 10,000 columns of data and not the 57 stated?

If you have 10,000 plus rows and want to loop based on that number use 1 instead of 2.
 
Upvote 0
My pleasure, thanks for the feedback. :cool:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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