Evaluate Variable At Run-Time

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
224
Office Version
  1. 365
Platform
  1. Windows
Hi,

Using Excel 365.

How do I evaluate a variable at run-time?
The code below is returning sp1, sp2, sp3
I am trying to evaluate the variable as Alpha, Bravo, Charlie,

Thanks
-w

VBA Code:
Sub foo2()

    Const sp1 As String = "Alpha"
    Const sp2 As String = "Bravo"
    Const sp3 As String = "Charlie"
    Const myv As String = "sp"
    Dim v As Variant
    Dim s As String
    Dim i As Long
    
    For i = 1 To 3
        s = myv & i
        Evaluate (s)
        Debug.Print s
    Next i
    
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try:

Debug.Print Evaluate(s)
 
Upvote 0
I am not sure it can be done that way. Why not ?

Option1
VBA Code:
Sub Option1()

    Const sp As String = "Alpha,Bravo,Charlie"
    
    Dim i As Long
    Dim s As Variant
     
    s = Split(sp, ",")
    
        For i = LBound(s) To UBound(s)
            Debug.Print s(i)
        Next i
End Sub

Option2
VBA Code:
Sub Option2()
        
    Dim i As Long
    Dim s As Variant
    s = Array("Alpha", "Bravo", "Charlie")
    
    For i = LBound(s) To UBound(s)
            Debug.Print s(i)
    Next i
    
End Sub
 
Upvote 0
Solution
I am not sure it can be done that way. Why not ?

Option1
VBA Code:
Sub Option1()

    Const sp As String = "Alpha,Bravo,Charlie"
   
    Dim i As Long
    Dim s As Variant
    
    s = Split(sp, ",")
   
        For i = LBound(s) To UBound(s)
            Debug.Print s(i)
        Next i
End Sub

Option2
VBA Code:
Sub Option2()
       
    Dim i As Long
    Dim s As Variant
    s = Array("Alpha", "Bravo", "Charlie")
   
    For i = LBound(s) To UBound(s)
            Debug.Print s(i)
    Next i
   
End Sub
Thanks Alex,
Much better!

Thanks,
-w
 
Upvote 0

Forum statistics

Threads
1,223,445
Messages
6,172,182
Members
452,447
Latest member
willsing5130

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