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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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,039
Messages
6,122,802
Members
449,095
Latest member
m_smith_solihull

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