Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
Hi All,
I want to create a small function (for example called printvar) that when it is called it prints the name of the variable that was passed to it in a message box. There may well be an obvious way of doing this but I don't know it. E.g. Sub Test MyText = "Hello" MsgBox PrintVar(MyText) End Sub The Result would then by "MyText" in a messagebox. Any ideas? Ta Andi |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
you want to go from recording macros to programming. good!.. my advice: but buy a book to learn VBA, understanding the basics makes it much more fun. Sub demo() test "Wow!" end sub Sub Test(sText as string) msgbox sText end sub -- keepITcool | www.XLsupport.com | keepITcool chello nl | amsterdam Andibevan wrote : Hi All, I want to create a small function (for example called printvar) that when it is called it prints the name of the variable that was passed to it in a message box. There may well be an obvious way of doing this but I don't know it. E.g. Sub Test MyText = "Hello" MsgBox PrintVar(MyText) End Sub The Result would then by "MyText" in a messagebox. Any ideas? Ta Andi |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
Already have books on it... If you re-read my post you will see that your
example code doesn't do what I wanted - I want to print the name of the variable, not the value of the variable I was just trying to find a way of reducing the amount of letters required "keepITcool" wrote in message ft.com... you want to go from recording macros to programming. good!.. my advice: but buy a book to learn VBA, understanding the basics makes it much more fun. Sub demo() test "Wow!" end sub Sub Test(sText as string) msgbox sText end sub -- keepITcool | www.XLsupport.com | keepITcool chello nl | amsterdam Andibevan wrote : Hi All, I want to create a small function (for example called printvar) that when it is called it prints the name of the variable that was passed to it in a message box. There may well be an obvious way of doing this but I don't know it. E.g. Sub Test MyText = "Hello" MsgBox PrintVar(MyText) End Sub The Result would then by "MyText" in a messagebox. Any ideas? Ta Andi |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
i see. reading is an art.. not my forte! i dont think it can be done. Function PrintVar(byref arg as variant) 'the argument is now called arg and it's pointer is set to the memorylocation of the "caller's" variable. getting at the memorylocation is easy with varptr() but i know no mechanism to get it's "name" in the caller's code. can you shed some light as to WHY you'd need it? also have a look at CallByName (available only in vba6) again it's not entirely what you ask (it applies to objects and their properties/methods) but you may find it usefull in a slightly different context -- keepITcool | www.XLsupport.com | keepITcool chello nl | amsterdam Andibevan wrote : Already have books on it... If you re-read my post you will see that your example code doesn't do what I wanted - I want to print the name of the variable, not the value of the variable I was just trying to find a way of reducing the amount of letters required Andibevan wrote : Hi All, I want to create a small function (for example called printvar) that when it is called it prints the name of the variable that was passed to it in a message box. There may well be an obvious way of doing this but I don't know it. E.g. Sub Test MyText = "Hello" MsgBox PrintVar(MyText) End Sub The Result would then by "MyText" in a messagebox. Any ideas? Ta Andi |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
I have been using message boxes to validate data at the end of a piece of
code and I end up with lots of lines like this:- Msgbox ("variable is " & variable") Thought it would be easier if I put Msgbox(Function(variable)) I am sure that there are better ways to do this but this works for me. Ta Andi "keepITcool" wrote in message ft.com... i see. reading is an art.. not my forte! i dont think it can be done. Function PrintVar(byref arg as variant) 'the argument is now called arg and it's pointer is set to the memorylocation of the "caller's" variable. getting at the memorylocation is easy with varptr() but i know no mechanism to get it's "name" in the caller's code. can you shed some light as to WHY you'd need it? also have a look at CallByName (available only in vba6) again it's not entirely what you ask (it applies to objects and their properties/methods) but you may find it usefull in a slightly different context -- keepITcool | www.XLsupport.com | keepITcool chello nl | amsterdam Andibevan wrote : Already have books on it... If you re-read my post you will see that your example code doesn't do what I wanted - I want to print the name of the variable, not the value of the variable I was just trying to find a way of reducing the amount of letters required Andibevan wrote : Hi All, I want to create a small function (for example called printvar) that when it is called it prints the name of the variable that was passed to it in a message box. There may well be an obvious way of doing this but I don't know it. E.g. Sub Test MyText = "Hello" MsgBox PrintVar(MyText) End Sub The Result would then by "MyText" in a messagebox. Any ideas? Ta Andi |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
On Thu, 16 Jun 2005 11:46:37 +0100, "Andibevan"
wrote: Hi All, I want to create a small function (for example called printvar) that when it is called it prints the name of the variable that was passed to it in a message box. There may well be an obvious way of doing this but I don't know it. E.g. Sub Test MyText = "Hello" MsgBox PrintVar(MyText) End Sub The Result would then by "MyText" in a messagebox. Any ideas? Ta Andi Is there some reason you need a separate function? ============ Sub Test() MyText = "Hello" MsgBox (MyText) End Sub =========== seems to work just fine. Or maybe I'm not understanding your question? --ron |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
hmm..
why do you need to debug with messageboxes? in case you skipped the chapter on debugging <G in the vbe open the "locals" window. set a breakpoint and.. check your variables. -- keepITcool | www.XLsupport.com | keepITcool chello nl | amsterdam Andibevan wrote : I have been using message boxes to validate data at the end of a piece of code and I end up with lots of lines like this:- Msgbox ("variable is " & variable") Thought it would be easier if I put Msgbox(Function(variable)) I am sure that there are better ways to do this but this works for me. Ta Andi "keepITcool" wrote in message ft.com... i see. reading is an art.. not my forte! i dont think it can be done. Function PrintVar(byref arg as variant) 'the argument is now called arg and it's pointer is set to the memorylocation of the "caller's" variable. getting at the memorylocation is easy with varptr() but i know no mechanism to get it's "name" in the caller's code. can you shed some light as to WHY you'd need it? also have a look at CallByName (available only in vba6) again it's not entirely what you ask (it applies to objects and their properties/methods) but you may find it usefull in a slightly different context -- keepITcool www.XLsupport.com | keepITcool chello nl | amsterdam Andibevan wrote : Already have books on it... If you re-read my post you will see that your example code doesn't do what I wanted - I want to print the name of the variable, not the value of the variable I was just trying to find a way of reducing the amount of letters required Andibevan wrote : Hi All, I want to create a small function (for example called printvar) that when it is called it prints the name of the variable that was passed to it in a message box. There may well be an obvious way of doing this but I don't know it. E.g. Sub Test MyText = "Hello" MsgBox PrintVar(MyText) End Sub The Result would then by "MyText" in a messagebox. Any ideas? Ta Andi |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
Sorry - as KeepItCool made the same mistake, my description must have been
bad. Your solution would put "Hello" in a message box. I am trying to get "MyText" in a message box "Ron Rosenfeld" wrote in message ... On Thu, 16 Jun 2005 11:46:37 +0100, "Andibevan" wrote: Hi All, I want to create a small function (for example called printvar) that when it is called it prints the name of the variable that was passed to it in a message box. There may well be an obvious way of doing this but I don't know it. E.g. Sub Test MyText = "Hello" MsgBox PrintVar(MyText) End Sub The Result would then by "MyText" in a messagebox. Any ideas? Ta Andi Is there some reason you need a separate function? ============ Sub Test() MyText = "Hello" MsgBox (MyText) End Sub =========== seems to work just fine. Or maybe I'm not understanding your question? --ron |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
On Thu, 16 Jun 2005 11:46:37 +0100, "Andibevan"
wrote: Hi All, I want to create a small function (for example called printvar) that when it is called it prints the name of the variable that was passed to it in a message box. There may well be an obvious way of doing this but I don't know it. E.g. Sub Test MyText = "Hello" MsgBox PrintVar(MyText) End Sub The Result would then by "MyText" in a messagebox. Any ideas? Ta Andi To add to my previous, if you wanted a separate function that returns a string to your sub, you could use something like: ================= Sub Test() mytext = "Hello" MsgBox printvar(mytext) End Sub Private Function printvar(ByVal str As String) As String printvar = str End Function ================== --ron |
#10
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
On Thu, 16 Jun 2005 13:12:48 +0100, "Andibevan"
wrote: Sorry - as KeepItCool made the same mistake, my description must have been bad. Your solution would put "Hello" in a message box. I am trying to get "MyText" in a message box No, your description was OK; I just saw "name" of variable and took it to mean "contents" of variable. I don't have an answer other than to define a set of variables which are the names of your variables, possible followed by the string ... is " --ron |
#11
Posted to microsoft.public.excel.programming
|
|||
|
|||
Create Function to display name of variable
Sub Test
MyText = "Hello" MsgBox MyText & vbcr & "World..!" End Sub "Andibevan" wrote in message ... | Hi All, | | I want to create a small function (for example called printvar) that when it | is called it prints the name of the variable that was passed to it in a | message box. There may well be an obvious way of doing this but I don't | know it. | | | E.g. | | Sub Test | MyText = "Hello" | | MsgBox PrintVar(MyText) | | End Sub | | The Result would then by "MyText" in a messagebox. | | Any ideas? | | Ta | | Andi | | |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Create a Random Variable | Excel Discussion (Misc queries) | |||
create new variable | Excel Worksheet Functions | |||
how to create a three variable chart. | Charts and Charting in Excel | |||
How do I create a one variable data table? | Excel Discussion (Misc queries) | |||
Excel VBA - MsgBox to display variable value? | Excel Programming |