Today, I ran into the weirdest bug I have ever seen in VBA, and I thought you all might find it interesting.
I'll post the workbook on Monday, but here's what I can tell you for now.
The code goes something like this:
Here's where things get funky: In debug mode, stepping through, everything works fine. In run-time mode, the array comes out... Empty
Things I figured out:
1) Using debug.print MyWB.Names(1).NameLocal right after the Workbook.Open outputs the correct value, so the workbook seems to be opening fine.
2) After the loop runs (in run-time mode), the value of intLoop is zero, indicating that the loop doesn't run at all.
3) Adding Application.Wait(Now()+TimeValue("00:00:01")) before the loop causes it to work correctly. I have no idea why this works.
Essentially, the loop doesn't trigger if there isn't a delay beforehand, and I have no clue why.
I'll post the workbook on Monday, but here's what I can tell you for now.
The code goes something like this:
Code:
Sub Example()
dim arrNames() as variant
dim intLoop as integer
dim intSize as integer: intSize = 0
Set MyWB = Workbook.Open([I].[/I]..)
For intLoop = 1 to MyWB.names.count
redim.preserve arrNames(intSize)
arrNames(intSize) = MyWB.Name(intLoop).NameLocal
intSize = intSize + 1
Next
[I]do stuff with names array[/I]
End Sub
Here's where things get funky: In debug mode, stepping through, everything works fine. In run-time mode, the array comes out... Empty
Things I figured out:
1) Using debug.print MyWB.Names(1).NameLocal right after the Workbook.Open outputs the correct value, so the workbook seems to be opening fine.
2) After the loop runs (in run-time mode), the value of intLoop is zero, indicating that the loop doesn't run at all.
3) Adding Application.Wait(Now()+TimeValue("00:00:01")) before the loop causes it to work correctly. I have no idea why this works.
Essentially, the loop doesn't trigger if there isn't a delay beforehand, and I have no clue why.