Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How 2 Execute Sheet1 Button_Click Code from a module function

If you have a button on Sheet1 that executes code written behind Sheet1 and
you want to call that same code from a function or macro in another module
how would you go about doing it?

--
~ SPARKER ~
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default How 2 Execute Sheet1 Button_Click Code from a module function

This assumes the command button was created from the control toolbox not the
forms toolbar. Your code will look something like

Private Sub CommandButton1_Click()

End Sub

Change it to

Public Sub CommandButton1_Click()

End Sub

Now you can reference it from other sheets or modules similar to

Sheet2.CommandButton1_Click

--
HTH...

Jim Thomlinson


"sparker" wrote:

If you have a button on Sheet1 that executes code written behind Sheet1 and
you want to call that same code from a function or macro in another module
how would you go about doing it?

--
~ SPARKER ~

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How 2 Execute Sheet1 Button_Click Code from a module function


Personally I would put the code behind the button click event into a
standard module as a subroutine. Then I would use the button click
event to call that subroutine. Then you can also call the same sub
from another function or macro.

ie, change this:

Private Sub CommandButton1_Click()
'Your code here
End Sub

to:

'In the code behind the sheet
Private Sub CommandButton1_Click()
ButtonCode
End Sub

'In a standard code module
Sub ButtonCode
'Your code here
End Sub

Then you can call the ButtonCode sub from any function of subroutine


--
bhofsetz
------------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...o&userid=18807
View this thread: http://www.excelforum.com/showthread...hreadid=381403

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default How 2 Execute Sheet1 Button_Click Code from a module function

In my opinion (for whatever that is worth) if the code for the button only
applies to this one sheet then It belongs in the sheet (where sparker has
it). If the code is more generic and could be used by many sheets and modules
then it belongs in a module. Depends entirely on the scope from my
standpoint, but I probably should have noted your solution also. Good point.
--
HTH...

Jim Thomlinson


"bhofsetz" wrote:


Personally I would put the code behind the button click event into a
standard module as a subroutine. Then I would use the button click
event to call that subroutine. Then you can also call the same sub
from another function or macro.

ie, change this:

Private Sub CommandButton1_Click()
'Your code here
End Sub

to:

'In the code behind the sheet
Private Sub CommandButton1_Click()
ButtonCode
End Sub

'In a standard code module
Sub ButtonCode
'Your code here
End Sub

Then you can call the ButtonCode sub from any function of subroutine


--
bhofsetz
------------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...o&userid=18807
View this thread: http://www.excelforum.com/showthread...hreadid=381403


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
code is not to work on sheet1 Allan R Jeffery New Users to Excel 3 August 22nd 06 02:22 PM
code in module A to not execute a Worksheet_SelectionChange sub of another module Jack Sons Excel Discussion (Misc queries) 4 December 11th 05 11:52 PM
Create a newworksheet with VBA code and put VBA code in the new worksheet module ceshelman Excel Programming 4 June 15th 05 04:37 PM
Use VB code to remove code in sheet1 WashoeJeff Excel Programming 4 January 25th 04 12:23 AM


All times are GMT +1. The time now is 01:47 AM.

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"