How to use cell reference for TimeValue in VBA

loloelpalomero

New Member
Joined
Sep 16, 2008
Messages
12
Hello all. I would swear that I have seen this kind of syntax for ontime somewhere:

Code:
Application.OnTime TimeValue([A1]), "colorbg_macro"
But if I try to use it, a Type Mismatch error 13 appears. If I use a real time value like next one the function works:

Code:
Application.OnTime TimeValue("13:10:00"), "colorbg_macro"
I am trying to use cell values for drawing a bar while time increases. Times are in cells and I would like to use the cells references as values for TimeValue.

I have been searching in the forum but I haven't found a similar case.

Help would be appreciated.

Thanks in advanced.
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This seemed to work:
Code:
Application.OnTime TimeValue(CStr(Format([A1].Value, "hh:mm:ss"))), "colorbg_macro"
 
Upvote 0
TimeValue takes a String as its argument. Excel serial times, no matter how the cell is formatted, are read as type Double.

Code:
Application.OnTime TimeValue(Range("A1").Text), "colorbg_macro"
will account for those users who use the 1904 date system.
 
Last edited:
Upvote 0
Hi again. I have finally used mikerickson's option, as I needed a code wich don't needed to enter a string. It works!

Thanks to both for your replies.

Learning Excel step by step thanks to people like you...
 
Upvote 0
How would someone use the below code? In what code window (Std, or event-type?)
What lines of code would proceed or follow the below? example, please..??

Application.OnTime TimeValue(Range("A1").Text), "colorbg_macro"
 
Upvote 0
Hi Jim. I have put this part in Workbook VBA:

Code:
Private Sub Workbook_Open()
    Application.OnTime TimeValue(Range("D3").Text), "colorbg03"
End Sub

The previous code could be entered in other class, like Worksheet_Change or something. It depends on when you would prefer to start the code to be executed (check the time).

And this in a Module:

Code:
Sub colorbg03()
Range("d3:f3").Interior.Color = RGB(255, 0, 0)
End Sub

In cell D3 there is a time string (i.e.: "13:10:00")

When D3 time is reached, cells D3 to F3 background turn to red.

I am using this code for some kind of schedulling system but I don't like alarms, message boxes and things like that. I prefer just a bar growing through cells to be easy to find the "to-do" things. Bar is growing horizontally and "to-do" things are just below in a few rows.

Hope it helps.
 
Upvote 0
When I open file with

Private Sub Workbook_Open()
Application.OnTime TimeValue(Range("A1").Text)
MsgBox "Hello - It's time"
'Application.OnTime TimeValue(Range("A1").Text), "my_Procedure"
End Sub

".OnTime" is highlited and I get error Compile error - Argument Not Optional

Why is that?
 
Upvote 0
The error occurred because there was no procedure inlcuded. This gives error:
Code:
Application.OnTime TimeValue(Range("A1").Text)
This does not:
Code:
Application.OnTime TimeValue(Range("A1").Text), "my_Procedure"
 
Upvote 0
I am not sure what you are trying to do. I suppose that you want to show a message when time reached. If that is right then you will have to do the following:

(Be aware that I am a newbie in excel so do at your own risk ;-) Anyway, I have tested it and works...

1) Insert a time in A1 like 13:10:00

2) Open VBA Editor and, in the project window, double click in "This Workbook" and type in the window:

Code:
Private Sub Workbook_Open()
Application.OnTime TimeValue(Range("A1").Text), "my_Procedure"
End Sub

3) Open a module or create a new one and type:

Code:
Sub my_Procedure()
MsgBox "Hello - It's time"
End Sub

Remember that you have choosen a Workbook_Open procedure so you will have to close and re-open your excel file for activating.
 
Upvote 0
Thought this code was my saviour but finding it's very hit and miss as to whether the time applied in my code will trigger my macros.
Would really appreciate some advice of more experienced people on here where I'm going wrong if possible.
I have several subs following MYBACKUP1 DOWN TO 180 ODD and want each backup to be triggered by time cells starting at cell z35 going down to Z180 but finding them very hit and miss, particularly if my sheet is resorted via times in column Z...

Code:
Private Sub Workbook_Open()  
       
    Application.OnTime TimeValue(Range("$Z$35").Text), "MYBACKUP1"    '  $Z$35  is the Trigger time I want for MYBACKUP1 to kick off
    Application.OnTime TimeValue(Range("$Z$36").Text), "MYBACKUP2"    '  $Z$36  is the Trigger time I want for MYBACKUP2 to kick off
End Sub

Code:
Sub MYBACKUP1()
'
 ' MYBACKUP1 Macro
' Macro recorded 6/9/2013 
' $Z$35 IS THE TRIGGER TIME i NEED FOR MYBACKUP1
' $Q$17 IS A CELL REFERENCED TO MY SYSTEM CLOCK AND SIMPLY HAS THE FORMULA =NOW() INSIDE IT.
' DATA1 IS THE DETAILS i NEED COPIED TO $BZ$35 WHEN MY SYSTEM CLOCK MATCHES THAT OF Z$35 

Application.ScreenUpdating = False
If Sheet1.Range("$Z$35").Value > Sheet1.Range("$Q$17").Value Then Exit Sub  
If ActiveSheet.Name <> "WhiteBoard" Then Exit Sub
If Sheet1.Range("$B$35") = "" And Sheet1.Range("$B$35") = "" Then Exit Sub
If Sheet1.Range("$BZ$35").Value <> "" Then Exit Sub
If Sheet1.Range("$Z$35").Value <= Sheet1.Range("$Q$17").Value Then

ActiveSheet.Unprotect
Range("DATA1").Offset(, 76) = Range("DATA1").Value      ' DATA1 Range copied to $BZ$35 here.
ActiveSheet.Protect
End If
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top