Refer to a constant

SabCie

New Member
Joined
Dec 20, 2010
Messages
24
Hello,

I want to do a small function in excel that will permit the user to retrieve information stored in a VBA module. I cannot find a function in VBA that will make VBA understand that this is a variable.

Here is my example:

Const Key199309 =20
Const Key199310 =25
Const Key199311 =30

Function Rates(ValYear As String, ValMonth As String)

Rates = "key" & ValYear & Month

End Function

This of course does not work...I tried to put a ME() but nothing worked...


So for example Rates(1993,09) would return me 20.


How can I achieve this!!

Thanks a lot
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You can't address variables that way.
One workaround would be to use a function rather than constants

Code:
Function Key(index as Long) as Long
    Select Case index
        Case 199309 
            Key = 20
        Case 199310 
            Key = 25
        Case 199311 
            Key = 30
    End Select
End Function

Function Rates(ValYear As String, ValMonth As String) as Long
    Rates = Key(Val(ValYear&Month))
End Function
 
Upvote 0
Thank you very much for responding!

What I find strange is that in a form you have the ability to do it. For example, in an old code I was able to make VBA recognise variable that way:


Dim Test(1 to 20) As Object

For I = 1 To 20
Set Test(I) = Form1("Label" & I)
Next I

Thanks
 
Upvote 0
In the form situation, you are building the string index to refer to a member of a Collection. (The Controls collection of the form, to be specific)
Rich (BB code):
Set Test(I) = Form1.Controls("Label" & I)

VBA constants are not members of a collection.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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