Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Evaluate a Variable Name from a String

Is it possible in VBA to determine a variable name from a string? For
example -

- - - - - -
Dim VariableName as String
Dim FavoriteNumber as Integer

FavoriteNumber = 55
VariableName = "FavoriteNumber"

msgbox {*}
- - - - - -

* indicating an expression that will return the value of the FavoriteNumber
variable, using the value of VariableName ("FavoriteColor") to evaluate the
FavoriteColor variable, and return the value of 55.

I know the example seems overly complicated (why not just use the
FavoriteNumber directly in the code?)

I have a class module with 65 variables, and I would like to place a
reference to them all in an Array, so that I can loop through them all and
assign values, yet I would like the values to still be updated if any of the
variables are updated outside of the array.

In other words, I would like an object or expression that references a
variable.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Evaluate a Variable Name from a String


Hello Developer of the Caribbean,

From what you describe your problem is more of a design problem. Havin
written many Class modules myself, the only reason I can see you woul
want to do this is beacuse you don't know enough about the language an
its structure yet to accomplish your goal. Why not post the code s
others can guide you in resolving these issues? If it is lengthy
attach the code in a zip file and upload it.

Sincerely,
Leith Ros

--
Leith Ros
-----------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846
View this thread: http://www.excelforum.com/showthread.php?threadid=48416

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Evaluate a Variable Name from a String

Since your variables are contained in a class module, you can use CallByName
method. Example below:

Class1:
Public FavoriteNumber As Integer

Module1:
Sub test()
Dim C As New Class1
Dim VariableName As String

C.FavoriteNumber = 55

VariableName = "FavoriteNumber"

MsgBox CallByName(C, VariableName, VbGet)
End Sub


"Developer of the Caribbean"
wrote in message
...
Is it possible in VBA to determine a variable name from a string? For
example -

- - - - - -
Dim VariableName as String
Dim FavoriteNumber as Integer

FavoriteNumber = 55
VariableName = "FavoriteNumber"

msgbox {*}
- - - - - -

* indicating an expression that will return the value of the
FavoriteNumber
variable, using the value of VariableName ("FavoriteColor") to evaluate
the
FavoriteColor variable, and return the value of 55.

I know the example seems overly complicated (why not just use the
FavoriteNumber directly in the code?)

I have a class module with 65 variables, and I would like to place a
reference to them all in an Array, so that I can loop through them all and
assign values, yet I would like the values to still be updated if any of
the
variables are updated outside of the array.

In other words, I would like an object or expression that references a
variable.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Evaluate a Variable Name from a String

Leith,

Thank you for responding!

Unfortunately, my class is over 500 lines of code long. Essentially, the 65
variables represent metadata fields (mostly strings and integers) that
together represent a job listing ("Client Name", "Job Type", "Work Order
Number", "Shipping Company".) The fields are public so other modules can
read and write directly into individual fields of the object.

The methods of the class perform group actions on all of the fields, such as
clearing them all, writing them all to a worksheet (using user customizable
columns stored in an array), updating corresponding fields in an
enterprise-wide database, or filling the the fields in the object from the
database.

All of the code works fine. It is just bulky. Each method hard codes all
of the field names - some methods clear all the fields, other fill all of the
fields. Since many fields in each method use the same logic, it would be
cleaner if there is a way to loop through the variables, but I would need
some sort of reference list or collection to loop through.

I am experienced with VBA, but my thinking on this issue may be complicated
by my knowledge of Java, where it is relatively easy to create an array of
variable references.

Any advice you can give would be very much appreciated! I am open to a
complete rethink.

"Leith Ross" wrote:


Hello Developer of the Caribbean,

From what you describe your problem is more of a design problem. Having
written many Class modules myself, the only reason I can see you would
want to do this is beacuse you don't know enough about the language and
its structure yet to accomplish your goal. Why not post the code so
others can guide you in resolving these issues? If it is lengthy,
attach the code in a zip file and upload it.

Sincerely,
Leith Ross


--
Leith Ross
------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=484164


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Evaluate a Variable Name from a String

Mark,

I will test this in the morning, but this looks like what I was searching for.

Thank you so much!

"Mark H. Shin" wrote:

Since your variables are contained in a class module, you can use CallByName
method. Example below:

Class1:
Public FavoriteNumber As Integer

Module1:
Sub test()
Dim C As New Class1
Dim VariableName As String

C.FavoriteNumber = 55

VariableName = "FavoriteNumber"

MsgBox CallByName(C, VariableName, VbGet)
End Sub


"Developer of the Caribbean"
wrote in message
...
Is it possible in VBA to determine a variable name from a string? For
example -

- - - - - -
Dim VariableName as String
Dim FavoriteNumber as Integer

FavoriteNumber = 55
VariableName = "FavoriteNumber"

msgbox {*}
- - - - - -

* indicating an expression that will return the value of the
FavoriteNumber
variable, using the value of VariableName ("FavoriteColor") to evaluate
the
FavoriteColor variable, and return the value of 55.

I know the example seems overly complicated (why not just use the
FavoriteNumber directly in the code?)

I have a class module with 65 variables, and I would like to place a
reference to them all in an Array, so that I can loop through them all and
assign values, yet I would like the values to still be updated if any of
the
variables are updated outside of the array.

In other words, I would like an object or expression that references a
variable.




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
How to evaluate string? Boban Excel Programming 4 November 4th 05 06:21 PM
Evaluate and execute a string Randall[_6_] Excel Programming 3 August 4th 05 05:19 PM
Evaluate string as a formula peacelittleone Excel Worksheet Functions 3 June 26th 05 06:20 PM
Evaluate using a variable - Help please SA3214 Excel Programming 11 April 11th 05 10:38 PM
how to evaluate the content of a string as if it was a formula cyrille de brébisson Excel Discussion (Misc queries) 5 December 6th 04 10:47 PM


All times are GMT +1. The time now is 07:59 AM.

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

About Us

"It's about Microsoft Excel"