How do I cycle through buttons in a FOR loop?

  • Thread starter Thread starter Morris
  • Start date Start date
M

Morris

I've got this piece of code which is obsiously not working:

For i = 1 To Worksheets.Count
TempForm.CommandButton & i & .Caption = Worksheets(i).Name
Next i

But I need to assign different captions based on sheet names.. How do
I achieve that?

Cheers,
Morris
 
try this
Sub getworksheetname()
For Each Myworksheet In ThisWorkbook.Worksheets
myname = Myworksheet.Name
Next Myworksheet

End Sub
 
Assuming TempForm is a UserForm with CommandButtons:

On Error Resume Next
For i = 1 To Worksheets.Count
TempForm.Controls("CommandButton" & i).Caption =
Worksheets(i).Name
Next i

Hth,
Merjet
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top