Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,311
Default Using a variable to select a CommandButton and setting the caption

I'm using a CommandButton from the Control Toolbox. How can I access this
control via VBA using a Variable.

Example:
i=1

I have CommandButton1.

I'd like to set the caption of CommandButton1 use the "i" variable for the
number of that CommandButton. I've done this before with a button on a
UserForm using something like:

UserForm3.Controls("CommandButton" & i).Caption = "Test"

So how can I do this with a CommandButton that is not on a UserForm?

Thanks,
Paul



--



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Using a variable to select a CommandButton and setting the caption

Hi Paul-

I know that a control on a worksheet is actually a member of the Shapes
collection (not the Controls collection). So you can loop through the shapes
and I tried:

Dim s As Shape
Dim cb As CommandButton

For Each s In Sheet1.Shapes
If s.Name = "CommandButton" & i Then
'Set cb = s
'cb.Caption = "Test"
End If
Next

But the Set statement doesn't work and that's why I commented it out...

So I guess that is a partial answer. Sorry that isn't much help.

John



"PCLIVE" wrote:

I'm using a CommandButton from the Control Toolbox. How can I access this
control via VBA using a Variable.

Example:
i=1

I have CommandButton1.

I'd like to set the caption of CommandButton1 use the "i" variable for the
number of that CommandButton. I've done this before with a button on a
UserForm using something like:

UserForm3.Controls("CommandButton" & i).Caption = "Test"

So how can I do this with a CommandButton that is not on a UserForm?

Thanks,
Paul



--




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Using a variable to select a CommandButton and setting the caption

See if this example gets you started...

Worksheets ("Sheet1")
X = 1
.OLEObjects("CommandButton" & X).Object.Caption = "First Button"
X = 2
.OLEObjects("CommandButton" & X).Object.Caption = "Second Button"
End With

--
Rick (MVP - Excel)


"PCLIVE" wrote in message
...
I'm using a CommandButton from the Control Toolbox. How can I access this
control via VBA using a Variable.

Example:
i=1

I have CommandButton1.

I'd like to set the caption of CommandButton1 use the "i" variable for the
number of that CommandButton. I've done this before with a button on a
UserForm using something like:

UserForm3.Controls("CommandButton" & i).Caption = "Test"

So how can I do this with a CommandButton that is not on a UserForm?

Thanks,
Paul



--




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,311
Default Using a variable to select a CommandButton and setting the caption

YES! That's what I was looking for.

Thanks.
Paul

--

"Rick Rothstein" wrote in message
...
See if this example gets you started...

Worksheets ("Sheet1")
X = 1
.OLEObjects("CommandButton" & X).Object.Caption = "First Button"
X = 2
.OLEObjects("CommandButton" & X).Object.Caption = "Second Button"
End With

--
Rick (MVP - Excel)


"PCLIVE" wrote in message
...
I'm using a CommandButton from the Control Toolbox. How can I access
this control via VBA using a Variable.

Example:
i=1

I have CommandButton1.

I'd like to set the caption of CommandButton1 use the "i" variable for
the number of that CommandButton. I've done this before with a button on
a UserForm using something like:

UserForm3.Controls("CommandButton" & i).Caption = "Test"

So how can I do this with a CommandButton that is not on a UserForm?

Thanks,
Paul



--






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,311
Default Using a variable to select a CommandButton and setting the caption

Rick, Thanks for your help on the previous question.
since you know just where I was coming from on that question, I'm tagging
this one on too.

When I press a CommandButton, is there a way to capture the caption
information of a command button that was just pressed and have it displayed
in, say AA1? I'd like this to happen without knowing which CommandButton
I'm pressing.

Range("AA1").Value= Pressed Command Button Caption

Is this possible?

Thanks,
Paul



--

"Rick Rothstein" wrote in message
...
See if this example gets you started...

Worksheets ("Sheet1")
X = 1
.OLEObjects("CommandButton" & X).Object.Caption = "First Button"
X = 2
.OLEObjects("CommandButton" & X).Object.Caption = "Second Button"
End With

--
Rick (MVP - Excel)


"PCLIVE" wrote in message
...
I'm using a CommandButton from the Control Toolbox. How can I access
this control via VBA using a Variable.

Example:
i=1

I have CommandButton1.

I'd like to set the caption of CommandButton1 use the "i" variable for
the number of that CommandButton. I've done this before with a button on
a UserForm using something like:

UserForm3.Controls("CommandButton" & i).Caption = "Test"

So how can I do this with a CommandButton that is not on a UserForm?

Thanks,
Paul



--






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
Commandbutton Caption WLMPilot Excel Programming 1 April 8th 08 07:26 PM
UserForm CommandButton Caption PCLIVE Excel Programming 3 January 29th 07 11:01 PM
Setting form caption based on global variable JNW Excel Programming 10 July 20th 06 10:47 PM
Change CommandButton Caption Claude Excel Programming 4 July 16th 04 04:18 PM
CommandButton.Caption Bob Phillips[_5_] Excel Programming 0 September 8th 03 08:15 AM


All times are GMT +1. The time now is 05:38 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"