In VBA are "Variable" Variable Userform Object Names Possible?

jardenp

Active Member
Joined
May 12, 2009
Messages
367
In a Userform I have three groups of maybe 30 text boxes, command buttons, radio buttons, etc. The groups are identical except for the number in the object names. For example, I have 3 text boxes: LastName_TB1, LastName_TB2, and LastName_TB3; or 3 command buttons: ProcessScan_CB1, ProcessScan_CB2, and ProcessScan_CB3. It's in groups because I have command buttons that copy certain data down to the next group, making it much faster for the person entering data.

At the bottom there is a command button to process the data. That command button will run a series of checks and data moves to each group separately.

Is there a way to write a script that I could run a 3-iteration loop on to change the object names or do I just need to make copies of the finished scripts and, say, replace all "TB1" with "TB2", "CB1" with "CB2", etc.? I'm thinking something along the lines of
VBA Code:
For i = 1 to 3
    ...
    LastName_TB&i.copy Sheets(Destination)...
    ...
Next i
Where "i" become part of the object name.

I mean...I can copy and replace...but it's probably 250 lines of code to "process" a group, any future change would have to be made in three places, sounds like a pain to get straight, reading through copies to make sure the find and replace went ok, etc.

Thank you!
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,833
Office Version
  1. 365
Platform
  1. Windows
You can do it like
VBA Code:
   Dim i As Long
   
   For i = 1 To 3
      Sheets("Destination").Range("F" & i).Value = Me.Controls("LastName_TB" & i)
   Next i
 
Solution

jardenp

Active Member
Joined
May 12, 2009
Messages
367
You can do it like
VBA Code:
   Dim i As Long
  
   For i = 1 To 3
      Sheets("Destination").Range("F" & i).Value = Me.Controls("LastName_TB" & i)
   Next i
Thank you, Fluff! That's exactly what I was looking for!
 

jardenp

Active Member
Joined
May 12, 2009
Messages
367
One quick follow up, if I may: does this also work on normal variable names? If not, is there a similar solution for those? I'm thinking of Integer, Date, etc. names.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,833
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

No it doesn't work for variables, however you could us an array
Rich (BB code):
   Dim aryNames As Variant
  
   aryNames = Array("Tom", "Dick", "Harry")
   Debug.Print aryNames(2)
 

jardenp

Active Member
Joined
May 12, 2009
Messages
367
How would I use that to get, say, a message boxes to display "2" then "3" here?
VBA Code:
Dim aryNames As Variant
Dim i As Integer
Dim num1 As Integer: num1 = 2
Dim num2 As Integer: num2 = 3

For i = 1 To 2

   aryNames = Array("num", i)
   MsgBox aryNames
Next i
I'm not familiar with how arrays work, so I'm just experimenting here.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,833
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Like
VBA Code:
Dim aryNum(1 To 2) As Long
Dim i As Integer

aryNum(1) = 2
aryNum(2) = 3
For i = 1 To 2
   MsgBox aryNum(i)
Next i
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,833
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,356
Messages
5,595,681
Members
414,009
Latest member
SNesbyCarr

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