Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Refering to an object with a variable

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)



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Refering to an object with a variable

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

--
Regards,
Tom Ogilvy


"Cory" wrote:

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)



  #3   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Refering to an object with a variable

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.


"Cory" wrote:

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)



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
refering to worksheet cell under shaoe object David Excel Programming 6 August 23rd 05 01:21 PM
Run-time error '91': "Object variable or With block variable not set Mike[_92_] Excel Programming 2 December 30th 04 10:59 AM
Cells.Find error Object variable or With block variable not set Peter[_21_] Excel Programming 2 May 8th 04 02:15 PM
refering to a range with a variable eyecalibrate[_2_] Excel Programming 2 March 1st 04 04:06 PM
Simple problem refering to variable in For ... Next loop ... Chris Excel Programming 1 July 15th 03 01:00 AM


All times are GMT +1. The time now is 11:23 AM.

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

About Us

"It's about Microsoft Excel"