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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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