excel vba - VBA: Get name of variable -
is there function or property variables name?
something like
msgbox myvariable.name
or
msgbox nameofvariable(myvariable)
that returns "myvariable"
when defined e.g. myvariable = "whatever"
? google brings questions variable referencing...
a possible class approach following (commented):
add class module
in vba ide
click insert-> class module
click view -> property window
enter
(name)
property textbox , type in "variable" (or whatever may prefer consistent in following steps)enter following code in
class
code paneoption explicit 'declare fields attached every instance of class public name string '<--| store name of variable you'll set object of class public value variant '<--| store value associated variable you'll set object of class 'declare `method` write `value` of object in named range named after `name` field sub writerange(optional wb variant) '<--| can pass reference of workbook named ranges want exploit if ismissing(wb) set wb = activeworkbook '<--| if no workbook reference passed active workbook assumed if typename(wb) = "workbook" '<-- check proper workbook reference being passed) on error resume next '<-- prevent unassigned named range throw error wb.names(name).referstorange.value = value '<--| write value of `value` filed of current instance in named range of passed workbook named after `name` filed of current instance end if end sub
exploit
variable
class in codeas example of exploiting
variable
class 3 variables with, say,string
value 1st,integer
value 2nd ,double
value 3rd, in module code pane enter following code:option explicit sub main() dim myvariable1 variable, myvariable2 variable, myvariable3 variable '<--| declare variables of type "variable": choose whatever name may want set myvariable1 = createvariable("myvariable1", "this string value") '<-- set 1st variable name (you must use same name variable!) , value (myvariable1 have `string`type value) set myvariable2 = createvariable("myvariable2", 10) '<-- set 2nd variable name (you must use same name variable!) , value (myvariable2 have `integer`type value) set myvariable3 = createvariable("myvariable3", 0.3)'<-- set 3rd variable name (you must use same name variable!) , value (myvariable3 have `double` type value) 'exploit `writerange` method of class write object value corresponding named range: must have set proper named ranges in active workbook myvariable1.writerange '<--| write string "this string value" in named range "myvariable1" of active workbook myvariable2.writerange '<--| write number '10' in named range "myvariable2" of active workbook myvariable3.writerange '<--| write number '0.3' in named range "myvariable3" of active workbook end sub ' helper function create object of `variable` class , initialize `name` , `value` properties function createvariable(name string, value variant) variable '<--| function returns object of `variable` class set createvariable = new variable '<--| creates new object of `variable` class createvariable '<--| refer newly created object , ... .name = name '<--| ... set `name` property ... .value = value '<--| ... , `value` property end end function
Comments
Post a Comment