My focus is to clean a specific column in a specific sheet every one hours.
I wrote this code but doesn't work.
Sub CleanColumn()
Worksheets("Calculation").Columns(2).ClearContents
Call test
End Sub
Sub test()
Application.OnTime Now + TimeValue("01:00:00"), "CleanColumn"
End Sub
Where I wronge?
Thanks in advance.
below code is working.
Public Const ClCol = "CleanColumn"
Sub CleanColumn()
'Change the range as per your req..
Sheet1.Range("A1").ClearContents
Call test
End Sub
Sub test()
'Once you change the below time it will be adjusted accordingly.
Application.OnTime Now + TimeValue("00:01:00"), ClCol
End Sub
Hope this helps..
cI got it to work by copying your code to Module1 in a workbook and changing to this:
Sub test()
Application.OnTime Now + TimeValue("01:00:o0"), "Module1.CleanColumn"
End Sub
I believe that you need to tell it where the subroutine CleanColumn resides
Since it is on the hour, you should explicitly define which workbook and which worksheet like:
Workbook("\\f2\folder\wb_time.xlsm").Worksheets("Sheet1").Columns(2).ClearContents
Related
I created macros in vba but they don't work.
In the ThisWorkbook code I wrote:
Private Sub Workbook_Open()
Worksheets("Sheet1").Activate
End Sub
In the code of Sheet1 (Sheet1) I wrote:
Private Sub Worksheet_Activate()
MsgBox ("hello")
End Sub
but when I open the file nothing happens ...
If Sheet1 is already active when you open the workbook, then
Worksheets("Sheet1").Activate
does exactly nothing. Because you cannot activate what is already active.
Test it by adding 2 Sheets Sheet1 and Sheet2. Then select Sheet2 save the file and close it. Now open it again, it will run Worksheets("Sheet1").Activate and this will trigger the Worksheet_Activate properly.
Also note that MsgBox ("hello") should be without parenthesis as it does not return a value to a variable: MsgBox "hello"
An alternative solution is:
Write in a module:
Public Sub Sheet1_Activate()
MsgBox "Sheet1 Activate"
End Sub
In Sheet1 write:
Private Sub Worksheet_Activate()
Sheet1_Activate
End Sub
And in ThisWorkbook write:
Private Sub Workbook_Open()
If ThisWorkbook.ActiveSheet.Name = "Sheet1" Then
Sheet1_Activate
Else
ThisWorkbook.Worksheets("Sheet1").Activate
End If
End Sub
The idea is to have a new procedure Sheet1_Activate that takes the actual code and is triggered by the Worksheet_Activate as well as by the Workbook_Open in case the sheet is already the active sheet.
Sub auto_open()
MsgBox "HueHueHue"
End Sub
You can't activate the same thing twice.
Im getting a Run-time error '1004':
Select method of range class failed
This only happens when I move away from the sheet (Bet Angel) that has the macro running to another sheet. The code is below:
Sub DeleteStatus()
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
I want the macro to run even if I am moving between different sheets in the workbook.
Since you are moving to another worksheets, you need to activate or select the target worksheet first.
Sub DeleteStatus()
Sheets("Bet Angel").Activate
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
If you want to go back to your original worksheet, you can activate it at the end of DeleteStatus()
Like this
Sub DeleteStatus()
Sheets("Bet Angel").Activate
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
Sheets("your original worksheet").Activate
End Sub
If you don't want to use activate, then you need to dim the worksheet first.
Like this
Sub DeleteStatus()
Dim ws As Worksheet
Set ws = Sheets("Bet Angel")
ws.Range("O6:O50").ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
I'm trying to close my workbook after a certain amount of time.
I'm using 10 seconds for now just to test it but it's not working automatically.
I have to run the code once by myself.
here's my code in module.
Public Sub fermeoutil()
Workbooks("OUTIL_CRN.xlsm").Save
Workbooks("OUTIL_CRN.xlsm").Close
Call test
End Sub
Sub test()
Application.OnTime Now + TimeValue("00:00:10"), "fermeoutil"
End Sub
In ThisWorkbook add procedure Workbook_Open with following code:
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:00:10"), "fermeoutil"
End Sub
In Module1 keep your current procedure fermeoutil() removing the call to test:
Public Sub fermeoutil()
Workbooks("OUTIL_CRN.xlsm").Save
Workbooks("OUTIL_CRN.xlsm").Close
End Sub
Your call to test (or Workbook_Open() as it is now called), is not needed, as you have left out the last argument of Application.OnTime, namely Schedule which is optional and has a default value of True meaning the event will be recurring. Not sure if that really is your meaning, since you are closing the wb.
VBA newbie here. I am looking for a way that will allow my excel file to automatically save after a delay of 1 min after data input.
For example:
User Inputs Data --> Timer Starts (1min)
5 seconds passes.
User inputs Data --> Timer Restarts (1min)
1 min passes.
Excel File Saves - until the user starts inputting data again
Any thoughts?
One possibility is to leverage the Workbook.SheetChange event and Application.OnTime. You'll also need a Public variable, ScheduledTime in the example below.
Every time any (non-chart) sheet is changed (e.g. via data entry):
Any previously scheduled save, as long as it's still within the one-minute window, is cancelled.
A new save is scheduled for one minute out.
So something like the following:
In the ThisWorkbook code module:
Option Explicit
Public ScheduledTime
Private Sub Workbook_SheetChange(ByVal Sh As Object, _
ByVal Target As Range)
On Error Resume Next
Application.OnTime EarliestTime:=ScheduledTime, Procedure:="SaveTheFile", Schedule:=False
On Error GoTo 0
ScheduledTime = Now + TimeValue("00:01:00")
Application.OnTime EarliestTime:=ScheduledTime, Procedure:="SaveTheFile"
End Sub
In a regular code module:
Public Sub SaveTheFile()
ThisWorkbook.Save
End Sub
You could just as well use the Worksheet Change event if you want to restrict this to a particular sheet.
I have a similar take on this to BigBen.
In ThisWorkbook module:
Option Explicit
Public SnapShot As String
Private Sub Workbook_Open()
StartTimer
End Sub
Sub StartTimer()
If SnapShot = vbNullString Then SnapShot = Now
If DateDiff("s", SnapShot, VBA.CStr(Now)) >= 10 Then ThisWorkbook.Save
RestartTimer
End Sub
Sub RestartTimer()
Application.OnTime Now + TimeValue("00:00:10"), "ThisWorkbook.StartTimer"
End Sub
And then in the worksheet you are monitoring:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
ThisWorkbook.SnapShot = Now
End Sub
I was trying to make digital watch in excel. So, when I will open a workbook then in a cell, lets say in E3 cell will show time and it will continue in every second so that it looks like a digital watch. I can do it in Access as access Form has On Timer event where I can write code to show time in a Textbox. Is there any alternative way in excel. I have tried below codes. But the codes need to click manually on Button1. Can I make it automatic to run the Button1_Click() sub in every second?
Sub Button1_Click()
Application.OnTime Now(), "RunningTime"
End Sub
Sub RunningTime()
Range("E3") = Format(Now(), "hh:mm:ss")
End Sub
I have also tried Workbook_Open() method but it runs once when I open the file.
Private Sub Workbook_Open()
Application.OnTime Now(), "RunningTime"
End Sub
There is no direct way to achieve it but you can apply some tricks to do that. First put below codes to a Module.
Public Times As Boolean
Sub TimerRun()
If Not Times Then Exit Sub
Application.OnTime Now() + TimeValue("00:00:01"), "TimerRun"
Range("E3").Value = Range("E3").Value + TimeValue("00:00:01")
End Sub
Sub TimerStop()
Times = False
End Sub
Then copy below codes and paste in Workbook_Open() event.
Private Sub Workbook_Open()
Range("E3").Value = Format(Now(), "HH:MM:SS")
Times = True
TimerRun
End Sub
If you want to make start or stop button so that you can start/stop timer manually by clicking on a button then put a form control command button and then assign TimerRun sub to start and assign TimerStop to stop timer.