ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Calling a subroutine with a command button (https://www.excelbanter.com/excel-programming/413671-calling-subroutine-command-button.html)

JonV

Calling a subroutine with a command button
 
Hello,
I want to run a subroutine, when a command button is clicked. The
subroutine is stored in Sheet1 under the Microsoft Excel Objects folder of my
project and the command button is stored under UserForm1 in the Forms folder
of my project. Is there a certain command needed to reference the other
folder?

This is the code I have right now (Guessing_game is the name of the
subroutine I want to cal).

Private Sub CommandButton1_Click()
Sheet1.Activate
Call Guessing_game
End Sub

When I run this, I get an error stating "Sub or Function not defined."

Does anyone know how I can do this?

Dave Peterson

Calling a subroutine with a command button
 
First, unless that Guessing_Game routine is related to only the code in that
worksheet's module, I'd move it to a general module and just call it normally.

But you can do:

Option Explicit
Private Sub CommandButton1_Click()
Call Sheet1.Guessing_Game
End Sub

Sheet1 is the codename for the sheet, right???


JonV wrote:

Hello,
I want to run a subroutine, when a command button is clicked. The
subroutine is stored in Sheet1 under the Microsoft Excel Objects folder of my
project and the command button is stored under UserForm1 in the Forms folder
of my project. Is there a certain command needed to reference the other
folder?

This is the code I have right now (Guessing_game is the name of the
subroutine I want to cal).

Private Sub CommandButton1_Click()
Sheet1.Activate
Call Guessing_game
End Sub

When I run this, I get an error stating "Sub or Function not defined."

Does anyone know how I can do this?


--

Dave Peterson

JonV

Calling a subroutine with a command button
 
I used your code and it worked. Thank-you very much.

"Dave Peterson" wrote:

First, unless that Guessing_Game routine is related to only the code in that
worksheet's module, I'd move it to a general module and just call it normally.

But you can do:

Option Explicit
Private Sub CommandButton1_Click()
Call Sheet1.Guessing_Game
End Sub

Sheet1 is the codename for the sheet, right???


JonV wrote:

Hello,
I want to run a subroutine, when a command button is clicked. The
subroutine is stored in Sheet1 under the Microsoft Excel Objects folder of my
project and the command button is stored under UserForm1 in the Forms folder
of my project. Is there a certain command needed to reference the other
folder?

This is the code I have right now (Guessing_game is the name of the
subroutine I want to cal).

Private Sub CommandButton1_Click()
Sheet1.Activate
Call Guessing_game
End Sub

When I run this, I get an error stating "Sub or Function not defined."

Does anyone know how I can do this?


--

Dave Peterson



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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com