Create Function to display name of variable

A

Andibevan

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
 
K

keepITcool

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 :
 
A

Andibevan

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



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 :
 
K

keepITcool

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 :
 
A

Andibevan

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


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 :
 
R

Ron Rosenfeld

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
 
K

keepITcool

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 :
 
A

Andibevan

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



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
 
R

Ron Rosenfeld

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
 
R

Ron Rosenfeld

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
 
A

Andrew

Sub Test
MyText = "Hello"
MsgBox MyText & vbcr & "World..!"
End Sub



| 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
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top