PC Review


Reply
Thread Tools Rate Thread

CommandButton and a variable

 
 
PCLIVE
Guest
Posts: n/a
 
      16th Oct 2006
Is it possible to reference a CommandButton by using a variable. For
example, I have CommandButton's 1 through 8. So the first one is
"CommandButton1", the second, "CommandButton2", etc.

These eight buttons perform the same actions with the exception of some
number references which correspond to the specific button that was pressed.
So instead of duplicating the code on each button, I'd like to know if there
is a way to have each button run the same module code, but a variable will
be used to set the numbers as appropriate.

Here is the code that is being used.

Private Sub CommandButton7_Click()
With Me.CommandButton7
.Caption = "Done 6"
.BackColor = &H800000
.ForeColor = &HFFFFFF
.Font.Bold = True
End With
Application.Calculation = xlCalculationManual
Range("AA1").Formula = "6"
Range("AG6").Formula = "1"
Application.Run "CreateWeeks"
End Sub


I've altered it to be used in a module as follows.

Sub WeekButtons()

With ActiveSheet.CommandButton7
.Caption = "Done " & wn 'this variable is set from the
CommandButton code
.BackColor = &H800000
.ForeColor = &HFFFFFF
.Font.Bold = True
End With

Application.Calculation = xlCalculationManual
Range("AA1").Formula = wn
Range("AG6").Formula = "1"
Application.Run "CreateWeeks"
End Sub

Is there a way to reference CommandButton7 as variable? For example, where
wn = 6:
With ActiveSheet.CommandButton & (wn + 1)

I know this doesn't work, but is there some way to do this?

Thanks,
Paul



 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      16th Oct 2006
With ActiveSheet.OleObjects("CommandButton" & (wn + 1)).Object

--
Regards,
Tom Ogilvy




"PCLIVE" wrote:

> Is it possible to reference a CommandButton by using a variable. For
> example, I have CommandButton's 1 through 8. So the first one is
> "CommandButton1", the second, "CommandButton2", etc.
>
> These eight buttons perform the same actions with the exception of some
> number references which correspond to the specific button that was pressed.
> So instead of duplicating the code on each button, I'd like to know if there
> is a way to have each button run the same module code, but a variable will
> be used to set the numbers as appropriate.
>
> Here is the code that is being used.
>
> Private Sub CommandButton7_Click()
> With Me.CommandButton7
> .Caption = "Done 6"
> .BackColor = &H800000
> .ForeColor = &HFFFFFF
> .Font.Bold = True
> End With
> Application.Calculation = xlCalculationManual
> Range("AA1").Formula = "6"
> Range("AG6").Formula = "1"
> Application.Run "CreateWeeks"
> End Sub
>
>
> I've altered it to be used in a module as follows.
>
> Sub WeekButtons()
>
> With ActiveSheet.CommandButton7
> .Caption = "Done " & wn 'this variable is set from the
> CommandButton code
> .BackColor = &H800000
> .ForeColor = &HFFFFFF
> .Font.Bold = True
> End With
>
> Application.Calculation = xlCalculationManual
> Range("AA1").Formula = wn
> Range("AG6").Formula = "1"
> Application.Run "CreateWeeks"
> End Sub
>
> Is there a way to reference CommandButton7 as variable? For example, where
> wn = 6:
> With ActiveSheet.CommandButton & (wn + 1)
>
> I know this doesn't work, but is there some way to do this?
>
> Thanks,
> Paul
>
>
>
>

 
Reply With Quote
 
PCLIVE
Guest
Posts: n/a
 
      16th Oct 2006
Thanks Tom,

That works pretty good. I have another dilema. Is there a way to carry a
variable over from a Private Sub to a Sub? For example, the CommandButton
sets the variable and then runs a Macro. I'm sure I can easily do this by
assigning the variable to a cell and then recall it from the macro, but I
didn't know if there was another way.

Thanks again.
Paul

"Tom Ogilvy" <(E-Mail Removed)> wrote in message
news:06F34209-9B5F-467C-BB62-(E-Mail Removed)...
> With ActiveSheet.OleObjects("CommandButton" & (wn + 1)).Object
>
> --
> Regards,
> Tom Ogilvy
>
>
>
>
> "PCLIVE" wrote:
>
>> Is it possible to reference a CommandButton by using a variable. For
>> example, I have CommandButton's 1 through 8. So the first one is
>> "CommandButton1", the second, "CommandButton2", etc.
>>
>> These eight buttons perform the same actions with the exception of some
>> number references which correspond to the specific button that was
>> pressed.
>> So instead of duplicating the code on each button, I'd like to know if
>> there
>> is a way to have each button run the same module code, but a variable
>> will
>> be used to set the numbers as appropriate.
>>
>> Here is the code that is being used.
>>
>> Private Sub CommandButton7_Click()
>> With Me.CommandButton7
>> .Caption = "Done 6"
>> .BackColor = &H800000
>> .ForeColor = &HFFFFFF
>> .Font.Bold = True
>> End With
>> Application.Calculation = xlCalculationManual
>> Range("AA1").Formula = "6"
>> Range("AG6").Formula = "1"
>> Application.Run "CreateWeeks"
>> End Sub
>>
>>
>> I've altered it to be used in a module as follows.
>>
>> Sub WeekButtons()
>>
>> With ActiveSheet.CommandButton7
>> .Caption = "Done " & wn 'this variable is set from the
>> CommandButton code
>> .BackColor = &H800000
>> .ForeColor = &HFFFFFF
>> .Font.Bold = True
>> End With
>>
>> Application.Calculation = xlCalculationManual
>> Range("AA1").Formula = wn
>> Range("AG6").Formula = "1"
>> Application.Run "CreateWeeks"
>> End Sub
>>
>> Is there a way to reference CommandButton7 as variable? For example,
>> where
>> wn = 6:
>> With ActiveSheet.CommandButton & (wn + 1)
>>
>> I know this doesn't work, but is there some way to do this?
>>
>> Thanks,
>> Paul
>>
>>
>>
>>



 
Reply With Quote
 
Tom Ogilvy
Guest
Posts: n/a
 
      17th Oct 2006
Private Sub Commandbutton1_Click()
Dim wn as Long
wn = 1
myroutine wn
End Sub



Sub MyRoutine(wn as Long)
Dim cb as MSForms.CommandButton
set cb = Activesheet.OleObjects("CommandButton" & wn).Object
' other code
End Sub

--
Regards,
Tom Ogilvy




"PCLIVE" <pclive(RemoveThis)@cox.net> wrote in message
news:(E-Mail Removed)...
> Thanks Tom,
>
> That works pretty good. I have another dilema. Is there a way to carry a
> variable over from a Private Sub to a Sub? For example, the CommandButton
> sets the variable and then runs a Macro. I'm sure I can easily do this by
> assigning the variable to a cell and then recall it from the macro, but I
> didn't know if there was another way.
>
> Thanks again.
> Paul
>
> "Tom Ogilvy" <(E-Mail Removed)> wrote in message
> news:06F34209-9B5F-467C-BB62-(E-Mail Removed)...
>> With ActiveSheet.OleObjects("CommandButton" & (wn + 1)).Object
>>
>> --
>> Regards,
>> Tom Ogilvy
>>
>>
>>
>>
>> "PCLIVE" wrote:
>>
>>> Is it possible to reference a CommandButton by using a variable. For
>>> example, I have CommandButton's 1 through 8. So the first one is
>>> "CommandButton1", the second, "CommandButton2", etc.
>>>
>>> These eight buttons perform the same actions with the exception of some
>>> number references which correspond to the specific button that was
>>> pressed.
>>> So instead of duplicating the code on each button, I'd like to know if
>>> there
>>> is a way to have each button run the same module code, but a variable
>>> will
>>> be used to set the numbers as appropriate.
>>>
>>> Here is the code that is being used.
>>>
>>> Private Sub CommandButton7_Click()
>>> With Me.CommandButton7
>>> .Caption = "Done 6"
>>> .BackColor = &H800000
>>> .ForeColor = &HFFFFFF
>>> .Font.Bold = True
>>> End With
>>> Application.Calculation = xlCalculationManual
>>> Range("AA1").Formula = "6"
>>> Range("AG6").Formula = "1"
>>> Application.Run "CreateWeeks"
>>> End Sub
>>>
>>>
>>> I've altered it to be used in a module as follows.
>>>
>>> Sub WeekButtons()
>>>
>>> With ActiveSheet.CommandButton7
>>> .Caption = "Done " & wn 'this variable is set from the
>>> CommandButton code
>>> .BackColor = &H800000
>>> .ForeColor = &HFFFFFF
>>> .Font.Bold = True
>>> End With
>>>
>>> Application.Calculation = xlCalculationManual
>>> Range("AA1").Formula = wn
>>> Range("AG6").Formula = "1"
>>> Application.Run "CreateWeeks"
>>> End Sub
>>>
>>> Is there a way to reference CommandButton7 as variable? For example,
>>> where
>>> wn = 6:
>>> With ActiveSheet.CommandButton & (wn + 1)
>>>
>>> I know this doesn't work, but is there some way to do this?
>>>
>>> Thanks,
>>> Paul
>>>
>>>
>>>
>>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a variable to select a CommandButton and setting the caption PCLIVE Microsoft Excel Programming 4 21st Oct 2008 06:22 PM
Ref Commandbutton WLMPilot Microsoft Excel Programming 4 20th Feb 2008 03:09 PM
CommandButton Simon Microsoft Outlook VBA Programming 3 13th Sep 2004 03:35 PM
CommandButton Hans Hofmann Microsoft Powerpoint 0 31st Aug 2004 01:57 PM
RE: CommandButton =?Utf-8?B?Y2hyaXM=?= Microsoft Excel Programming 1 15th Apr 2004 01:01 AM


Features
 

Advertising
 

Newsgroups
 


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