ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How do I cycle through buttons in a FOR loop? (https://www.excelbanter.com/excel-programming/384270-how-do-i-cycle-through-buttons-loop.html)

Morris[_2_]

How do I cycle through buttons in a FOR loop?
 
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


joel

How do I cycle through buttons in a FOR loop?
 
try this
Sub getworksheetname()
For Each Myworksheet In ThisWorkbook.Worksheets
myname = Myworksheet.Name
Next Myworksheet

End Sub

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

Cheers,
Morris



merjet

How do I cycle through buttons in a FOR loop?
 
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



All times are GMT +1. The time now is 06:58 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com