Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default multiple command buttons leading to 1 event

Hi,
I have checked the site:
http://j-walk.com/ss/excel/tips/tip44.htm

but the difference with my problem is that I dont have a user form. I
just have an excel sheet with some command buttons on it. When you
click any command button, I want the name of the caption on that
command button to be selected so that i can sort data in another sheet
using that caption.

I know i should be able to modify the above code somehow, but don't
know what to put instead of "For Each ctl In UserForm1.Controls". I
want this to be a very basic and quick thing. If I wanted to be fancy
(which I don't) I would just create an access database.

Thanks
Kate

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default multiple command buttons leading to 1 event

Each of those commandbuttons from the Control toolbox toolbar has its own click
event. You can just pass what you want to the common routine.

For instance, this passes the commandbutton itself:

(behind the worksheet)
Option Explicit
Private Sub CommandButton1_Click()
Call DoTheSort(Me.CommandButton1)
End Sub
Private Sub CommandButton2_Click()
Call DoTheSort(Me.CommandButton2)
End Sub
Private Sub CommandButton3_Click()
Call DoTheSort(Me.CommandButton2)
End Sub

In a general module:
Option Explicit
Sub DoTheSort(CMDBtn As MSForms.CommandButton)
Dim myStr As String
myStr = CMDBtn.Caption
MsgBox myStr
End Sub

====================
An alternative is to use the button from the Forms toolbar and use a common
routine in a general module. Then assign this macro to each of the buttons.

Option Explicit
Sub DoTheSort()
Dim myBTN As Button
Set myBTN = ActiveSheet.Buttons(Application.Caller)
MsgBox myBTN.Caption
End Sub





Kate wrote:

Hi,
I have checked the site:
http://j-walk.com/ss/excel/tips/tip44.htm

but the difference with my problem is that I dont have a user form. I
just have an excel sheet with some command buttons on it. When you
click any command button, I want the name of the caption on that
command button to be selected so that i can sort data in another sheet
using that caption.

I know i should be able to modify the above code somehow, but don't
know what to put instead of "For Each ctl In UserForm1.Controls". I
want this to be a very basic and quick thing. If I wanted to be fancy
(which I don't) I would just create an access database.

Thanks
Kate


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default multiple command buttons leading to 1 event

And I made a typo on the 3rd commandbutton. I passed it the 2nd one.

Be more careful than I was!

Dave Peterson wrote:

Each of those commandbuttons from the Control toolbox toolbar has its own click
event. You can just pass what you want to the common routine.

For instance, this passes the commandbutton itself:

(behind the worksheet)
Option Explicit
Private Sub CommandButton1_Click()
Call DoTheSort(Me.CommandButton1)
End Sub
Private Sub CommandButton2_Click()
Call DoTheSort(Me.CommandButton2)
End Sub
Private Sub CommandButton3_Click()
Call DoTheSort(Me.CommandButton2)
End Sub

In a general module:
Option Explicit
Sub DoTheSort(CMDBtn As MSForms.CommandButton)
Dim myStr As String
myStr = CMDBtn.Caption
MsgBox myStr
End Sub

====================
An alternative is to use the button from the Forms toolbar and use a common
routine in a general module. Then assign this macro to each of the buttons.

Option Explicit
Sub DoTheSort()
Dim myBTN As Button
Set myBTN = ActiveSheet.Buttons(Application.Caller)
MsgBox myBTN.Caption
End Sub

Kate wrote:

Hi,
I have checked the site:
http://j-walk.com/ss/excel/tips/tip44.htm

but the difference with my problem is that I dont have a user form. I
just have an excel sheet with some command buttons on it. When you
click any command button, I want the name of the caption on that
command button to be selected so that i can sort data in another sheet
using that caption.

I know i should be able to modify the above code somehow, but don't
know what to put instead of "For Each ctl In UserForm1.Controls". I
want this to be a very basic and quick thing. If I wanted to be fancy
(which I don't) I would just create an access database.

Thanks
Kate


--

Dave Peterson


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default multiple command buttons leading to 1 event

Thank you, yes this will work, but I was hoping to avoid having to
have code for each button individuallly because I have about 50
buttons... :)

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default multiple command buttons leading to 1 event

Maybe using the button from the Forms toolbar would be easier?

Kate wrote:

Thank you, yes this will work, but I was hoping to avoid having to
have code for each button individuallly because I have about 50
buttons... :)


--

Dave Peterson


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
REPLACE command drops leading zeros Guy Kerr Excel Discussion (Misc queries) 5 April 4th 23 12:40 PM
Using REPLACE command removes leading zeros Guy Kerr Excel Discussion (Misc queries) 2 October 15th 09 05:23 PM
Leading Zero and the change command mmccoog Excel Worksheet Functions 2 June 12th 07 09:37 PM
q: is this possible in vba - multiple command buttons.. Clinton M James[_2_] Excel Programming 1 March 14th 07 03:07 AM
Control Buttons vs. Command Buttons Robert Gibson Excel Programming 1 October 13th 03 04:33 PM


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