nemethv
nemethv

Reputation: 47

Dynamically query content of variables with index in the name

I have three string variables, thisTemplateFrame1Type, hisTemplateFrame2Type, thisTemplateFrame3Type.

They hold values such as PVCV, ABS_Var etc.

I have a loop that goes through these 3 (but n as it can be 1 to 3) variables and then based on a Select Case I want it to do things.

Example:

for i = 1 to {defined number of Frames}
   Select Case thisTemplateFrame1Type
      Case Is = "PVCV"
         [do stuff]
...
   End Select
Next i

Now this is tedious.

What I need instead is something like:

for i = 1 to {defined number of Frames}
   Select Case CStr("thisTemplateFrame" & meFrameCounter & "Type") 
  'this above should evaluate to say thisTemplateFrame1Type, thisTemplateFrame2Type...
      Case Is = "PVCV"
         [do stuff]
...
   End Select
Next i

That solution doesn't work because CStr("thisTemplateFrame" & meFrameCounter & "Type") always equals thisTemplateFrame1Type as a value but that basically means thisTemplateFrame1Type <> PVCV so the Case block moves on.

How do I pass the value properly?

Upvotes: 0

Views: 60

Answers (1)

QHarr
QHarr

Reputation: 84455

You cannot dynamically construct variable names in that way. You can however reframe (boom boom) your structure and loop an array of frames

Option Explicit
Public Sub test()
    Dim frames(), frame As Variant, thisTemplateFrame1Type As String, thisTemplateFrame2Type As String, thisTemplateFrame3Type As String
    thisTemplateFrame1Type = "A"
    thisTemplateFrame2Type = "B"
    thisTemplateFrame3Type = "C"

    frames = Array(thisTemplateFrame1Type, thisTemplateFrame2Type, thisTemplateFrame3Type)

    For frame = LBound(frames) To UBound(frames)

        Select Case frames(frame)
        Case "A"

        Case "B"

        Case "C"
        End Select

    Next
End Sub

Upvotes: 2

Related Questions