How to: Cell value = Variable name

mtheriault2000

Well-known Member
Joined
Oct 23, 2008
Messages
826
Hello

I got a blank

Range of cells contain as the value, the name of a variable to be read

EX:
A1 = Market
A2 = Scale
A3 = ToDo

Market, Scale, ToDo has been declared as string variable.

I want to built a standardized string using the value of the cells as the named of the variable

oString = Market & "," & Scale & "," & Todo

How do I use the value of a cell as the name of a variable to be read?

Hope I'm clear enough :)

Martin
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
marka87uk !

OUps, I was not well understood

As mentioned, the purpose of this exercise, is to built a standardized string of data. Named of variables, reside in cells.

Here is the list of the variables name that i want to use to build my string
Adding new information will be easy as all I would need to modify will be this list. Each cell containing named of a variable.

Infomation
Market
Scale_S
Todo
QTY_S
QTYLimit_S
EntryPrice_S
StopPrice_S
LimitPrice_S
ProfitTarget_S
TrailingPip_S
TrailingDollar_S


If Variable
Market = "Eur.Usd"Scale = "3 Minutes"

resulting string should be = "Eur.Usd, 3 Minutes"

If Variable
Market = "Gbp.Usd"
Scale = "1 Hour"

resulting string should be = "Gbp.Usd, 1 Hour"

I want to use the value of the cell as the name of my variable. My actual code is not working
Code:
  Comma = ","
   oRowCounter = 1
   OrderString = ""
   Information_S = Range("Information").Value
   'Information = "Information"
   
   While Information_S <> ""
      Information_S = Range("Information").Offset(oRowCounter, 0).Value
      OrderString = OrderString & Information_S.Value & Comma
      oRowCounter = oRowCounter + 1
    Wend
 
Upvote 0
Unresolved yet Re: How to: Cell value = Variable name

Bumps

Thank Vog (Peter) for the clarification on the forum rules... Understood
http://www.mrexcel.com/forum/showpost.php?p=2802410&postcount=2

I will use another wording. Hope I will be clearer
HTML:
A cell contain the name of a declared variable to be read. How do i use the content of a cell as the name of a variable

EX: 
variable:
Market = "Eur.Usd"
Scale = "3 Minutes"


Cell A1 = "Market"
Cell A2 = Scale

I want a string to be built using content of the variable name contained in cell A1 and contained in cell A2

String = Market & "," & Scale
String = "Eur.Usd,3 mintutes" 

I want to use the value of a cell as the name of the variable to be read

If I inverese the value of cell A1 and A2 then the string will equal:
A1 = "Scale"
A2 = "Market"
String = "3 Minutes,Eur.Usd"

How do I use the text value of a cell as the name of a variable to be read. The text could represent the name of an Integer Variable also

Hope I'm clearer
Martin
 
Upvote 0
Hi Martin

You cannot do it. You cannot use a string value and then get the reference to the variable.

2 options
- use names
- use a collection

Ex. using a collection, you initialise the collection with the values and the respective keys. You can then use the values of cells as keys to get the values.

Run Test()

Code:
Option Explicit
 
Dim Coll As Collection
 
Sub Init()
 
Set Coll = New Collection
Coll.Add "Eur.Usd", "Market"
Coll.Add "3 Minutes", "Scale"
End Sub
 
Sub Test()
Dim s As String
 
' Write "Market" in A1 and "Scale" in A2
 
Init
 
s = Coll(Range("A1")) & "," & Coll(Range("A2"))
MsgBox s
 
s = Coll(Range("A2")) & "," & Coll(Range("A1"))
MsgBox s
End Sub
 
Upvote 0
Thanks PGC

I never used Collection, I will see that

Also, I thing I will save my value in a Named Range cell. This way, I would be able to address my value using my list of variables.

Martin
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top