VBA Userform Question - Naming Commandbuttons

orangefury90

New Member
Joined
Sep 21, 2013
Messages
11
Hello,

This is my first post and I am glad to help / have some of my questions answered concerning Excel and VBA.

Anyway, my problem is that I have a userform called "update_log_form" and I have a bunch of buttons where for each button I want to change the captions.

GOAL: easily change button captions.

2dahvlx.jpg


So far, I have changed the names of each commandbutton to "R1C1", "R1C2" etc.

My code looks like this right now:

'CHANGE BUTTON NAMES
update_log_form.R1C1.Caption = 1
update_log_form.R1C2.Caption = 2
update_log_form.R1C3.Caption = 3
update_log_form.R1C4.Caption = 4
update_log_form.R1C5.Caption = 5

update_log_form.R2C1.Caption = 6
update_log_form.R2C2.Caption = 7
update_log_form.R2C3.Caption = 8
update_log_form.R2C4.Caption = 9
update_log_form.R2C5.Caption = 10

' ... etc

I also want to add other things such as if the button is hidden or not using .visible = true or .visible = false but I see this taking a long, long time.

If I have all the information I need already, is there any code which could easily transfer this information to each button with a for loop and an array?

I am thinking something like the code below could help (with 24 being the total number of buttons and "0 to 2" being 3 possible slots of information concerning each button. However, I can't seem to get the red code below to reference the buttons I want (or any button).

Dim button_array(0 To 24, 0 To 2) as string

'Update button captions with information from array
for i = 1 to 5
for j = 1 to 5
update_log_form.R(i)C(j).caption = button_array(i,0)
next j
next i

Hope somebody can help and Thanks in advance!!!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
If anyone is interested, I figured it out. Here is the code when deciding if the command button is hidden or not:

Dim button As msforms.CommandButton

For i = 1 To 5
For j = 1 To 5
Set button = update_log_form.Controls("R" & i & "C" & j)
button.Visible = True
Next j
Next i
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,926
Members
449,479
Latest member
nana abanyin

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