View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Andrew[_56_] Andrew[_56_] is offline
external usenet poster
 
Posts: 130
Default How to enumerate buttons on user form

Hello,
I have a userform with up to 20 buttons. Upon opening I want the userform to read the name of each sheet and assign each sheet name to the caption of a button. Then I want the buttons to be used for navigation. I have a code which works, but I had to write out 20 cases for 20 possible buttons. Below is an example of my code for naming button 1.

Private Sub UserForm_Initialize()
If Worksheets.Count = 1 Then
CommandButton1.Visible = True
CommandButton1.Caption = Worksheets(1).Name
Else
CommandButton1.Caption = ""
CommandButton1.Visible = False
End If

I'd like to be able to do this without using the button names such as CommandButton1. I'd like to have all of the buttons in an array and then write this out as a for loop. Such as:

For k=1 to worksheets.count
CommandButton(k).Visible = True
CommandButton(k).Caption = Worksheets(k).Name
Next

How can this be done? Can I make an array of userform objects and index them?

thanks