Changing multiple button texts in a loop?

steve2008

New Member
Joined
Mar 16, 2011
Messages
3
Hi, I have 120 buttons on a form named commandbutton1 - commandbutton120

I have an Excel 97 sheet which will contain the texts I would like displayed on these buttons.

On opening I would like these values to be pasted into the buttons.

I can do this fine by writing 120 lines of code but was hoping there would be a way to do it in a for loop.

something like this (Note the code is wrong, i'm just trying to explain my aim.)

Code:
 for i=1 to 120
 commandbutton & i . value = activecell.value
 range("activecell.offset(0,1)").select
next


I have several attributes I wish to assign for each button, so would have to edit a lot of lines if this is not possible.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi,

Try:

Code:
    For Each cb In Me.Controls
        cb.Caption = ActiveCell.Value
        ActiveCell.Offset(0, 1).Select
    Next
 
Upvote 0
Edit:

Forgot to add a check to see if it's a command button, otherwise it will change everything.

Code:
    For Each cb In Me.Controls
        If TypeName(cb) = "CommandButton" Then
            cb.Caption = ActiveCell.Value
            ActiveCell.Offset(0, 1).Select
        End If
    Next
 
Upvote 0
Since you have a naming convention:
Code:
for i=1 to 120
Me.Controls("commandbutton" & i).Caption = activecell.offset(0, i - 1).value
next i
 
Upvote 0
I have added more command buttons of the same type at the bottom of the main list of 120. but they are for a different form.

Currently I have it coded like

Code:
for i=1 to 120
Till("commandbutton" & i).Caption = activecell.offset(0, i - 1).value
next i

where Till is the name of my form.
The me.controls command doesn't work here, but I would like it to, then would it change the rest of the buttons on the other forms just by changing i's range?

Alternatively I have been trying to create a sub routine which would get passed the forms name and the button number and set that button up.
something like the below, but that doesn't work.

Code:
sub buttonsetup ( byval formid as string, byval buttonid as integer)

formid("commandbutton" & buttonid).Caption 
   = activecell.offset(0, buttonid - 1).value
end sub

with the current codded section changing to

Code:
for i=1 to 120
buttonsetup till, i
next i
 
Upvote 0
It would be:
Code:
Till.controls("commandbutton" & I).caption = ...
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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