[VBA] how to return nominal name (not content) of a variable?

smallxyz

Active Member
Joined
Jul 27, 2015
Messages
393
Office Version
  1. 2021
Platform
  1. Windows
Code:
[I]Sub[/I] Test[I]([/I][I])[/I]
    Var1 = 1
    Var2 = 2

    Msgbox VarName[I]([/I]Var1[I])[/I]
    Msgbox VarName[I]([/I]Var2[I])[/I]

[I]End Sub[/I]






[I]Function[/I] VarName[I]([/I][I])[/I]




[I]End Function[/I]


Preferably, the above msgbox will result in pop-up window with "Var1" and "Var2", instead of 1, 2.
Is it possible?

Thanks a lot!
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I'm not sure why you would want this, but since you are coding the variable name in the msgbox, why not just put quotes round it? i.e.
Code:
    Msgbox VarName[I]("Var1")[/I]
    Msgbox VarName[I]([/I]"Var2"[I])[/I]
 
Last edited:
Upvote 0
I'm not sure why you would want this, but since you are coding the variable name in the msgbox, why not just put quotes round it? i.e.
Code:
    Msgbox VarName[I][I][I]([/I][/I]"Var1"[I][I])[/I][/I][/I]
    Msgbox VarName[I][I][I]([/I][/I][/I]"Var2"[I][I][I])[/I][/I][/I]
Hi jmacleary,
I wish to make a function to print out the name of variable and also its content.

e.g.
A = 100
B = 200
C = "zzz"
PrintVariable(A, B, C)

A message box will show up, giving following lines of message:

variable content
---------------------
A 100
B 200
C "zzz"
 
Last edited:
Upvote 0
AFAIK there is no way of doing that.
It maybe possible use Class modules, but unlikely to be simple.
 
Upvote 0
OK. What you could do is create a 2 dimensional array with the first element being the name, and the second element being the value, and then pass that array to your function.
 
Upvote 0
Hi

If you want both the name and the value of a variable one option is to use a dictionary.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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