Accessing Public sub in ThisWorkbook module - vba

I have VBA code in the Sheet1 module and I want to call a sub procedure in that Sheet1 module when the workbook open so I do:
Private Sub Workbook_Open()
Call MyMacro
End Sub
behind SHeet1 I have
Public Sub MyMacro()
........
End Sub
When the workbook opens I get the error:
sub or function not defined "call GetReutersData"
How can I call MyMacro from the Open() event?
I need to have the MyMacro code in the sheet1 module just becuase that's the way it has to be. I cannot create a new module.

Private Sub Workbook_Open()
sheet1.MyMacro
End Sub

Please try this
Write the below code in Thisworkbook
Private Sub Workbook_Open()
Call Sheet1.MyMacro
End Sub
'----- Sheet 1 Code ---
Public Function MyMacro()
MsgBox "hi"
End Function
Is Absolutely Working on My system

Related

Call public sub each day at specific time without reopening the file again

I have this public sub defined in Module 1 of my excel workbook
Public Sub AutoUpdate()
Application.OnTime TimeValue("22:40:00"), "GetTime"
End Sub
And in my workbook code i have this to call this AutoUpdate method.
Public Sub Workbook_Open()
Call AutoUpdate
End Sub
My problem is that when i open the file it ways the time to be 22:40:00 and it calls "GetTime" but then on the next day the excel either crashes or the function is not called again.
Is this Application.OnTime method working for each day without needing to reopen ( refresh ) the workbook again.
Try using the following code:
Private Sub Workbook_Open()
Call DailyRun
End Sub
DailyRun code:
Sub DailyRun()
Application.OnTime TimeValue("22:40:00"), "GetTime"
Application.OnTime TimeValue("23:59:00"), "DailyRun"
End Sub
The DailyRun macro calls itself at the end of each day to create new Ontime event.
If you want to run the macro using Windows Task Scheduler, add your macro to Workbook Open event:
Private Sub Workbook_Open()
Application.OnTime TimeValue("22:40:00"), "GetTime"
Me.Close
End Sub
Don't forget to close your workbook.

In VBA, can a workbook object reference a method written in one of its worksheets?

Edited to provide more information.
Base Question:
Is it possible for a workbook to have access to a method written in one of its worksheets in VBA?
Real World Reason:
I need to have a method run on workbook launch (Workbook_Open() event) however, that method has a few requirements which makes modules not viable:
Needs to maintain state
Needs to store data
Needs to be referenced from both the workbook, and the worksheets in said workbook
Psuedocode example:
myWorkbook
Private Sub Workbook_Open()
Call myWorkbook.Sheet("worksheet1").helloWorld
End Sub
worksheet1
Public Sub printHelloWorld()
'Store "Hello World" so we can use later, this method needs to maintain state
Dim helloWorld As String
Set helloWorld = "Hello World"
MsgBox(helloWorld)
End Sub
Yes, this can be done. Both of the below work fine.
Sub TestWithExplicitWorksheet()
wsTest.TestMessage
End Sub
Sub TestWithWorksheetsCollection()
ThisWorkbook.Worksheets("Sheet1").TestMessage
End Sub
wsTest Code:
Sub TestMessage()
Debug.Print "Success"
End Sub
A Worksheet is more or less a Predeclared Class Module. So if you can do it with a Class, you should be able to do it with a Worksheet. Since the Worksheets collection of the Workbook object returns (if found) a Worksheet object, you can call the method on this returned object (if it's the correct object and contains the sub being called).
I would put the code below in a module
Public Sub printHelloWorld()
MsgBox("Hello World")
End Sub
and the code in myWorkBook will look like
Private Sub Workbook_Open()
printHelloWorld
End Sub
Yes, you can use the sheet CodeName (you can also change it in the sheet object properties):
Private Sub Workbook_Open()
Sheet1.printHelloWorld() ' full name is VBAProject.Sheet1.printHelloWorld()
End Sub

Sub Not Defined Error: Calling a Macro in Another Sheet

I'm trying to call a sub from another sub and am getting a Sub Not Defined error.
I'm trying to run a macro that hides a row in Sheet4 when a specific cell in Sheet1 changes.
Here is my code for Sheet1:
Public Sub HiddenValidations()
If [O18]=true then call test
End Sub
Here is my code for Sheet4:
Public Sub test()
Rows("7").entirerow.hidden=true
End Sub
Thank you for your help!
-Kim
You need to qualify your macro name:
Public Sub HiddenValidations()
If [O18]=true Then Sheet4.test
End Sub
(And there is no need for the Call to invoke a macro - as far as I know, it exists only for backward-compatibility reasons.)

Trigger code after inactivity [duplicate]

This question already has answers here:
Automatically close Workbook after inactivity
(2 answers)
Closed 7 years ago.
I want my WB to trigger some code after inactivity time (set by me, naturally). I could only find code to close the WB after inactivity time, but I want the code to do something else, different from closing the WB. I found this code for closing the WB:
This Workbook module:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
stop_Countdown
ThisWorkbook.Save
End Sub
Private Sub Workbook_Open()
start_Countdown
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
stop_Countdown
start_Countdown
End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
stop_Countdown
start_Countdown
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
stop_Countdown
start_Countdown
End Sub
Regular module:
Option Explicit
Public Close_Time As Date
Sub start_Countdown()
Close_Time = Now() + TimeValue("00:00:10")
Application.OnTime Close_Time, "close_WB"
End Sub
Sub stop_Countdown()
Application.OnTime Close_Time, "close_WB", , False
End Sub
Sub close_wb()
ThisWorkbook.Close True
End Sub
In what part of the code should I introduce the events I want the WB to do, after inactivity, instead of closing the WB?
You need to make changes in the Regular module.
Instead of passing the String close_wb() in your Application.OnTime function call, you have to specify the name of the procedure containing whatever you want to perform.
Here is the code to get you started:
Option Explicit
Public Inactivity_Time As Date
Sub start_Countdown()
Inactivity_Time = Now() + TimeValue("00:00:10")
Application.OnTime Inactivity_Time, "my_procedure" ' <- Notice that I changed the name of the procedure here
End Sub
Sub stop_Countdown()
On Error Resume Next
Application.OnTime Inactivity_Time, "my_procedure", , False ' <- And here too.
On Error GoTo 0
End Sub
Sub my_procedure()
' The code to perform you event goes here
End Sub
Please check here for more details on the Application.OnTime method.
EDIT: After some test, it appears that you can't call stop_Countdown() in the Workbook_BeforeClose sub procedure: It throws an error. According to this post, in your Workbook module, you have to replace the procedure Workbook_BeforeClose with the following one:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Close_WB
End Sub
And add the following procedure:
Public Sub Close_WB()
stop_CountDown
ThisWorkbook.Close SaveChanges:=True
End Sub

Calling a private VBA subroutine with Parameter

I have a VBA Add-In which is supposed to add functionality to several worksheets. I have a class to listen for worksheet events and trigger macros. This works but I can't seem to call private macros with parameters from it.
My event listener is in a class module and is this:
Option Explicit
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Application
End Sub
Private Sub App_SheetChange(ByVal Sh As Object, ByVal Source As Range)
On Error GoTo Finish
App.EnableEvents = False
Call Application.Run("Worksheet_Change", "Source")
Finish:
App.EnableEvents = True
End Sub
It is initialized on opening the workbook in a module like so:
Sub Auto_Open()
Set clsAppEvents = New clsApplicationEvents
End Sub
And the macro I'm trying to call is in a separate module again and takes the form:
Private Sub Worksheet_Change(ByVal Target As Range)
'Do some work on range
End Sub
I've tried the following ways of calling the macro and none have worked so far:
Call Application.Run("Worksheet_Change", "Source")
Application.Run "Worksheet_Change", "Source"
Application.Run "Worksheet_Change" "Source"
The arguments to Run don't all have to be strings, so
Application.Run "Worksheet_Change", "Source"
Should be
Application.Run "Worksheet_Change", Source