Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / September 2006

Tip: Looking for answers? Try searching our database.

Refering to an object with a variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cory - 28 Sep 2006 17:05 GMT
I have a userform with a number of Command Buttons (Button1, Button2,
etc...).  Before the form loads, I would like to change the caption of each
of the buttons (with the use of a loop) to the contents of certain cells.  
Can anyone help me with the syntax is to do so.  For example:

For x = 1 to 20
ProjectForm.("Button" & x).Caption = Sheet1.Cells(2, x + 2)
Tom Ogilvy - 28 Sep 2006 17:59 GMT
That is pretty much it.  You just need to package it in the initialize event
in the userforms code module

Private Sub Userform_Initialize()
Dim x as Long
Dim cb as MSForms.CommandButton
for x = 1 to 20
 set cb = me.Controls("Button" & x)
 cb.Caption = Sheet1.Cells(2, x + 2).Value
Next
end sub

Signature

Regards,
Tom Ogilvy

> I have a userform with a number of Command Buttons (Button1, Button2,
> etc...).  Before the form loads, I would like to change the caption of each
[quoted text clipped - 3 lines]
> For x = 1 to 20
> ProjectForm.("Button" & x).Caption = Sheet1.Cells(2, x + 2)
JMB - 28 Sep 2006 18:17 GMT
Try

For x = 1 To 20
ProjectForm.Controls("Button" & x).Caption = Sheet1.Cells(2, x + 2)
Next x

I'm assuming "Button" refers to the commandbutton object names, not the
current caption(s).  Unless they changed it in subsequent versions, VBA's
default name for its commandbuttons are CommandButton1, CommandButton2, etc.  
If you get an object not found error - you'll need to verify this.

> I have a userform with a number of Command Buttons (Button1, Button2,
> etc...).  Before the form loads, I would like to change the caption of each
[quoted text clipped - 3 lines]
> For x = 1 to 20
> ProjectForm.("Button" & x).Caption = Sheet1.Cells(2, x + 2)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.