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
Related
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'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.)
I need to create a timer in Excel 2016 VBA editor that will go-off every second and display a message.
I have a simple code that will do just that; however when I run the code a message box appears and then an error follows. My code is presented below:
Private Sub Workbook_Open()
RunEveryTwoMinutes
End Sub
Sub RunEveryTwoMinutes()
MsgBox ("Stop!")
Application.OnTime Now + TimeValue("00:00:01"), "RunEveryTwoMinutes"
End Sub
The error I receive states:
"Cannot run the macro 'C:user|generic\Book1.xlsm'. The macro may not be available in this workbook or all macros may be disabled.
I am not sure what is occurring. I simply need to find out a way to create a one second timer that initiates as soon as the code initializes.
Place this code in your workbook code module:
Private Sub Workbook_Open()
RunEveryTwoMinutes
End Sub
and place this code in a standard code module:
Sub RunEveryTwoMinutes()
MsgBox ("Stop!")
Application.OnTime Now + TimeValue("00:00:01"), "RunEveryTwoMinutes"
End Sub
Alternatively, you can have all the code in your Workbook code module, but you need to qualify the macro name:
Private Sub Workbook_Open()
RunEveryTwoMinutes
End Sub
Sub RunEveryTwoMinutes()
MsgBox ("Stop!")
Application.OnTime Now + TimeValue("00:00:01"), "ThisWorkbook.RunEveryTwoMinutes"
End Sub
I believe the first method is preferable.
I am trying to only run a set of macros if a sheet doesn't already exist. I have a macro that creates a sheet and combines data from two sheets into it, and another that formats the new sheet. Since it needs to run on workbook open, I can't have it recreating the sheet again and again. I have been trying the following, but it gives the error: "sub or Function not defined":
Private Sub Workbook_Open()
If SheetExist("MyNewSheet") Then
End Sub
Else
Combine
Format
End Sub
You aren't doing anything if the sheet exists, so change your test.
Private Sub Workbook_Open()
If Not SheetExist("MyNewSheet") Then
Combine
Format
End If
End Sub
Function SheetExist(sheetname As String) As Boolean
SheetExist = True ' replace this with code from link below
End Function
Use the answers here: Excel VBA If WorkSheet("wsName") Exists for examples of functions that determine whether the sheet exists.
Yea, the problem is "End Sub" should be "Exit Sub" You can also use the solution above/below.
Your fixed code would be:
Private Sub Workbook_Open()
If SheetExists("MyNewSheet") Then
Exit Sub
Else
Combine
Format
End If
End Sub
Also:
Public Function SheetExists(ByVal WorksheetName As String) As Boolean
On Error Resume Next
WorksheetExists = (Sheets(WorksheetName).Name <> "")
On Error GoTo 0
End Function