0

Is is possible to use a simple variable name to refer to a location in a VBA array?

I thought the location might be an object so I tried doing it kind of like this:

Sub ArrayRef()
    
    Dim X As Integer
    Dim ArrayWithQuiteALongName(1 To 3) As Integer
    Dim A As Object
    
    For X = 1 To 3
    
        Set A = ArrayWithQuiteALongName(X)
        
        A = X * 10
        
        A = A * 10
        
    Next X
    
    Debug.Print ArrayWithQuiteALongName(1) = 100
    Debug.Print ArrayWithQuiteALongName(2) = 200
    Debug.Print ArrayWithQuiteALongName(3) = 300
    
End Sub

In the example I would expect a Debug.Print output of

TRUE
TRUE
TRUE

But this doesn't work and I can't seem to find any information on if it's possible

Flewker
  • 33
  • 3
  • 1
    *Is is possible to use a simple variable name to refer to a location in a VBA array?* Nope. You can store the value of an array index in a variable, but arrays are not objects so you cannot set them like you want to. Anyways, why you would want to do that? If you call frequently same indexs of an array, make a secondary sub or something. Could you clarify what are you trying to do? – Foxfire And Burns And Burns Feb 24 '22 at 10:01
  • https://stackoverflow.com/questions/28326429/vba-pass-array-by-reference-and-modify-contents - this one might be of interest – Vityata Feb 24 '22 at 10:33

0 Answers0