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.)
Related
If I want to write a Sub that positions a UserForm relative to the Application object that displays it, How would I go about doing it?
I want to write a sub that goes like this:
Sub PositionForm(WhichForm as Object)
WhichForm.Left = <WhichForm Application Object>.Left
End Sub
I understand that there are many workarounds to this. I am interested in knowing whether there is a way to getting that reference.
in Excel the following works:
Sub PositionForm(WhichForm As Object)
WhichForm.Left = Application.Left
End Sub
to be called from any UserForm code as:
Private Sub CommandButton2_Click()
... any code
PositionForm Me
... any code
End Sub
My VBA code is too large and I'm trying to make smaller SUBS so the error won't come up, but then the error "Ambigoius name" pops up. I've tried to rename my subs...
Ex.
Private Sub worksheet_calculate()
Range("I9").Interior.Color=Range("AK9").Display.Format.Interior.Color
end sub
Private Sub worksheet_calculate2()
Range("J9").Interior.Color=Range("AQ9").Display.Format.Interior.Color
end sub
...when I rename the other subs as shown in the example it doesn't do anything, only the original work properly. How do I rename them so they can work properly?
In my understanding, worksheet_calculate is the predefined name of the subroutine, triggered by the event when the worksheet is recalculated.
You can define and call other private subs from it.
like
Private Sub worksheet_calculate()
rem sub body
CalculateSub1 pars 'variant one
Call CalculateSub1(pars) 'variant two
rem sub body
End Sub
Sub CalculateSub1(pars)
Rem Sub body
End Sub
Just insert the below line of codes at the end of your main sub which is "worksheet_calculate"
Call worksheet_calculate2
Call worksheet_calculate3
Call worksheet_calculate4
Call worksheet_calculate4
.
.
.
Call worksheet_calculaten
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
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
I have contents in column K that is constantly updating throughout the day. I want to copy the values of specific cells at specific times, and paste them into column F. What I lack is the VBA code to copy the contents at a specific time.
Based on chris neilsen's comment (I was not aware of Application.OnTime).
You can do the following (the code below goes in the ThisWorkbook module):
Option Explicit
Private Sub Workbook_Open()
Call ScheduleTask
End Sub
Public Sub ScheduleTask()
Application.OnTime TimeValue("15:00:00"), "ThisWorkbook.Execute"
End Sub
Public Sub Execute()
Debug.Print "Executing task", Now
'Place your code here
Call ScheduleTask
End Sub