Evaluate Variable At Run-Time

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
223
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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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,215,537
Messages
6,125,386
Members
449,221
Latest member
DFCarter

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