Can anyone help me with the codes and explain why my code dont work.
Define "doesn't work". If you get an Excel error message, what is it (
#NAME ?)? If you get a VBA error message, what is it? Or do you simply mean that you never see the "tested" Msgbox?
Function alarm (selectedcell as range)
sc=selectedcell.row
application.ontime Timevalue( cells(sc,1).text)," message"
End function
I am suspicious of the fact that things are not capitalized per VBA editor auto-corrections. It seems that you did not copy-and-paste from the VBA editor. Consequently, you might have unconsciously corrected the very error(?) that you are asking about.
What module did you enter the VBA function into?
It must be in a normal module (click Insert > Module), not a worksheet or ThisWorkbook module. Otherwise, Excel does not recognize the VBA function, and it should display a
#NAME error in the worksheet.
Cells(sc,1) is the cell in
column A of the row that contains selectedcell. Is that your intention?
What does Cells(sc,1).Text return?
What does Now() and TimeValue(Cells(sc,1).Text) return?
Be sure to format those values with 15 significant digits; for example, Format(Now(), "0.00000000000000E+0"). That's 14 zeros after the decimal point.
Note that TimeValue(...) returns only the time part, not also the date part. If the time part is before Now(), it refers to that time of day of the next day. If the time part is the same as Now() truncated to the second, it might refer to that time of day of the next day if, by coincidence, Now() is sufficiently close to the next second, and that second occurs before OnTime completes the scheduling of the event. That is called a "race condition".
It might be more reliable to write CDate(Cells(sc,1).Text), if the cell displays date as well as time.