Show a string by index part of name

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,713
I've some strings called "MyEncode1", "MyEncode2", "MyEncode3" etc and
passing 1 2 or 3 to a function (as n), wanting to see the one corresponding to n.

It won't work! I can go to the immediate window and debug print e.g. MyEncode2 fine
but debug print MyEncode & n fails.

There's probably a simple solution but it eludes me.
 

Some videos you may like

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
16,411
I would use an array of strings. Something like.

Code:
Sub aTest()
    Dim MyEncode() As String, n As Integer
    
    ReDim MyEncode(1 To 3)
    MyEncode(1) = "String 1"
    MyEncode(2) = "String 2"
    MyEncode(3) = "String 3"
    
    n = 2
    MsgBox MyEncode(n)
End Sub

M.
 

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,713
Hi Marcelo.. after some playing around, I ended up with something very similar (but using Array).
Thanks for confirming there's no other method..
 

Watch MrExcel Video

Forum statistics

Threads
1,123,079
Messages
5,599,636
Members
414,326
Latest member
kfg1287

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
Top