Indirect Reference to a Variable in VBA

Arul.rajesh

Active Member
Joined
Sep 20, 2011
Messages
285
I have a list of values which are names of arrays

Something like

Code:
 CDS=Array(CIPR,CIPW,PTPA,CSRP,CITR,CPER,CPCT,CPSC,CW9D,VMLE,VMAD,AUTH,AUTE,AUTB,AUTL,NMRC,NMRE,NMRF,NMFR,CDTF,DTFA,CDTE,CNOF,CFWA,CDED,CDES,DEDR,CCOB,PEOB,CCOD,BMAX,MAXS,CNCS,CNPI,CFAX,CAPP,DEFA,CPEE,CPFL,CFWU,CFWE,CCAP,AUTW,CITW,CIPE,CDUP,CPNL,BMBS,ADDI,POSR,CLIC,CLIA,COIN,HOSP,OONP,POST,PPMX,PPMD,PROV)


Each four digit string is an array.

The user selects an option from a ComboBox

I need to Set the value of a another variable to an element in the array referenced by the "CODE" the user selects.

The problem is the selected text is a string.

I can loop through all the codes and check if the user has selected which "CODE", if I have the name of the array as the first element in the array.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
What is user selecting in ComboBox ? Can you please provide me one example with data?
 
Upvote 0
What is user selecting in ComboBox ? Can you please provide me one example with data?

The user selects an option like this I can get the four letter code by
<a href="http://www.imagehousing.com/image/872518"><img border="0" src="http://img1.imagehousing.com/51/9045c9ebb370d11fc1439b37354498ff.jpg" alt="Free Image Hosting"></a><br><a href="http://www.imagehousing.com/image/872518">Free Image Hosting</a><br>

Code:
code=left(codebox.text,4)


now CPER=Array("dos","DSO",1,"Ins Name")

but I want the array CPER if the user selects CPER in the above example not the string CPER
 
Last edited:
Upvote 0
Let me rephrase my question

Like the way indirect function works in EXCEL

I need a VBA indirect function which would reference the variable stored in the value of another variable.


Code:
arg1=array(2,3,4)

arg2="arg1"

n=VBAINDIRECT(arg2) should assign the array arg1 to n

I need that VBAINDIRECT function
 
Last edited:
Upvote 0
you can try this,


Code:
code = Left(codebox.Text, 4)
 
Select Case code
   Case "CPER": CPER = Array("dos", "DSO", 1, "Ins Name")
   'Case "CPCT": CPCT = Array("data1", "data2", "data3", "data4")
 End Select

By default all those array variables would be variant.

Hope this helps!
 
Upvote 0
Sorry, you have to be explicit when using variables in VBA. You can't use strings in their place and there is no equivalent of Excel's INDIRECT.
 
Upvote 0
you can try this,


Code:
code = Left(codebox.Text, 4)
 
Select Case code
   Case "CPER": CPER = Array("dos", "DSO", 1, "Ins Name")
   'Case "CPCT": CPCT = Array("data1", "data2", "data3", "data4")
 End Select

By default all those array variables would be variant.

Hope this helps!



Perfect, Actually that even saves me assigning the arrays in the beginning of the SUB. Thanks a Lot.

I am from Hyderabad

Maybe we could catch up at the Metallica concert at the end of this month in Bangalore, I will be there. Ping me at arul.rajesh"at"gmaildotcom
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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