PC Review


Reply
Thread Tools Rate Thread

Calling sub from CommandButton knowing which sheet is active

 
 
gtslabs
Guest
Posts: n/a
 
      20th Apr 2007
I have many sheets with a command button on it using the same code.
I want to change this so I have one code.

I put that code in a module and made it PUBLIC

Public Sub Process()
' do stuff
client = Me.Range("C4")
' do stuf
end sub


my Command button on each sheet now had this code:
Private Sub CommandButton2_Click()
Call process
end sub


The problem I have is at the "ME" statement gives me an error -
"Invalid use of ME keyword"
The subroutine takes inforrmation from the sheet with the command
button and puts it on a different sheet. How do I code so the sheet
the code is called from is the active sheet or it knows its the active
sheet?

 
Reply With Quote
 
 
 
 
Norman Jones
Guest
Posts: n/a
 
      20th Apr 2007
Hi G,

Try something like:

'=============>>
Private Sub CommandButton2_Click()
Call Process(Me)
End Sub

Public Sub Process(SH As Worksheet)
' do stuff
client = SH.Range("C4").Value
' do stuf
End Sub
'<<=============


---
Regards,
Norman




"gtslabs" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have many sheets with a command button on it using the same code.
> I want to change this so I have one code.
>
> I put that code in a module and made it PUBLIC
>
> Public Sub Process()
> ' do stuff
> client = Me.Range("C4")
> ' do stuf
> end sub
>
>
> my Command button on each sheet now had this code:
> Private Sub CommandButton2_Click()
> Call process
> end sub
>
>
> The problem I have is at the "ME" statement gives me an error -
> "Invalid use of ME keyword"
> The subroutine takes inforrmation from the sheet with the command
> button and puts it on a different sheet. How do I code so the sheet
> the code is called from is the active sheet or it knows its the active
> sheet?
>



 
Reply With Quote
 
gtslabs
Guest
Posts: n/a
 
      20th Apr 2007
Thanks Norman that worked.

How can I access that SUB from each of the 50 sheets I already have
made before I implemented this new code?

I tried copying the Command button to another sheet but it left the
code blank.
I also tried putting it in the Macro list to call it from each sheet
but the "Me" gave me and error also.

I remember vaugly that I if I select multiple sheets I can somehow
loop thru them all.

 
Reply With Quote
 
Norman Jones
Guest
Posts: n/a
 
      20th Apr 2007
Hi G,

'-------------------
How can I access that SUB from each of the 50 sheets I already have
made before I implemented this new code?

I tried copying the Command button to another sheet but it left the
code blank.
I also tried putting it in the Macro list to call it from each sheet
but the "Me" gave me and error also.

I remember vaugly that I if I select multiple sheets I can somehow
loop thru them all.
'-------------------

Rather than inserting a new CommandButton on 50+
sheets, why not insert a new button on a toolbar and
assign the required the requred macro to the toolbar
button. In this way, you could use the ActiveSheet
property to identify the sheet.


---
Regards,
Norman


 
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
How to ID calling worksheet if it isn't the active sheet? cate Microsoft Excel Programming 2 30th Mar 2010 01:30 AM
sheet selection without knowing name evais Microsoft Excel Programming 2 14th Nov 2008 02:54 PM
run macro on a CommandButton from another sheet Corey Microsoft Excel Programming 1 1st Nov 2007 12:36 AM
Copy from active sheet and paste into new sheet using info from cell in active Ingve Microsoft Excel Programming 3 23rd Jan 2006 09:57 PM
How save active workbook w/o knowing name =?Utf-8?B?Sm9obg==?= Microsoft Excel Programming 3 20th Jan 2006 12:16 AM


Features
 

Advertising
 

Newsgroups
 


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