Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() 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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
code is not to work on sheet1 | New Users to Excel | |||
code in module A to not execute a Worksheet_SelectionChange sub of another module | Excel Discussion (Misc queries) | |||
Create a newworksheet with VBA code and put VBA code in the new worksheet module | Excel Programming | |||
Use VB code to remove code in sheet1 | Excel Programming |