Show a string by index part of name

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
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.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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.
 
Upvote 0
Hi Marcelo.. after some playing around, I ended up with something very similar (but using Array).
Thanks for confirming there's no other method..
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,495
Members
449,088
Latest member
Melvetica

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