How do I permanently change the value of a variable in an array? I wrote some code that illustrates my problem:
Option Explicit
Dim var1 As Boolean, var2 As Boolean, var3 As Boolean
Sub Test()
Dim arrTest As Variant
Dim i
arrTest = Array(var1, var2, var3)
For Each i In arrTest
Debug.Print i
Next
For i = LBound(arrTest) To UBound(arrTest)
arrTest(i) = True
Next
For Each i In arrTest
Debug.Print i
Next
Test2
End Sub
Sub Test2()
Debug.Print "var1 in Sub Test2 : " & var1
If var1 Then
Debug.Print "Hello"
End If
End Sub
The output in the direct window is:
False
False
False
True
True
True
var1 in Sub Test2 : False
The first six lines make sense to me because I changed the value of the variables in Test. However, it obviously wasn't permanent or it was confined to that one sub and so "Hello" in Test2 didn't get printed.
How do I change this? I've read that for each loops are read-only, but why does the for-loop not work?
