Using varibles and object references in Userforms

GiraffetheGeek

Board Regular
Joined
May 25, 2011
Messages
58
I have a user form that has 80 text boxes and 20 control buttons that have similar names that vary with numbers.

ie WB_TxtBox_NameWeek1Appt1, and WB_TxtBox_NameWeek1Appt2 etc
ie Btn_Week1Appt1, Btn_Week1Appt2, Btn_Week2Appt1 etc.

I want to be able to use a varible when making references to these object in code,

ie Having a WeekCount varible and then instead of
Code:
WB_TxtBox_NameWeek1Appt1.Text

I tried to use
Code:
WeekCount = 1
ApptCount =1
WB_TextBox_NameWeek & WeekCount & Appt & ApptCount.Text

This obviously didn't work as the syntax is wrong.

Can someone let me know what the correct syntax is?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hello,

Try something like:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Click()<br><br><SPAN style="color:#00007F">Dim</SPAN> WeekCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> ApptCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> varTextBox <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    WeekCount = 1<br>    ApptCount = 1<br>    varTextBox = "WB_TxtBox_NameWeek" & WeekCount & "Appt" & ApptCount<br>    <br>    Me.Controls(varTextBox).Text = "Hello"<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,544
Members
452,925
Latest member
duyvmex

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