Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a Random Variable [email protected] Excel Discussion (Misc queries) 3 August 25th 09 09:18 AM
create new variable Angie Excel Worksheet Functions 4 January 23rd 07 08:26 PM
how to create a three variable chart. [email protected] Charts and Charting in Excel 2 May 24th 06 12:28 AM
How do I create a one variable data table? prelll91 Excel Discussion (Misc queries) 0 February 27th 05 07:47 PM
Excel VBA - MsgBox to display variable value? BruceAtkinson[_5_] Excel Programming 2 June 4th 04 08:32 PM


All times are GMT +1. The time now is 03:50 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"