Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default How to enumerate buttons on user form

Hi Andrew,

Am Tue, 19 Jun 2012 05:23:59 -0700 (PDT) schrieb Andrew:

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


try:
Private Sub UserForm_Initialize()
Dim cmd As Control
Dim i As Integer

i = 1
For Each cmd In Me.Controls
If Left(cmd.Name, 4) = "Comm" _
And i <= Sheets.Count Then
cmd.Caption = Sheets(i).Name
cmd.Visible = True
End If
i = i + 1
Next cmd

End Sub


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default How to enumerate buttons on user form

On Tuesday, June 19, 2012 6:23:59 AM UTC-6, Andrew wrote:
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


Sorry I took so long to reply. Thank you very much for this nice concise code.

I appreciate it.

AG
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
user form and radio buttons pswanie Excel Programming 1 January 8th 08 09:02 PM
User form Command Buttons jhyatt Excel Discussion (Misc queries) 3 September 25th 07 04:28 PM
User Form Option & Command Buttons Information Hog[_2_] Excel Programming 1 August 19th 05 10:00 PM
Enumerate form items Kurt[_7_] Excel Programming 4 June 28th 05 10:26 PM
2 sets of option buttons on one user form? Pal Excel Programming 3 April 28th 04 02:45 PM


All times are GMT +1. The time now is 02:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"