Passing a cell value to a function

Vishak

New Member
Joined
Aug 20, 2017
Messages
1
I am looking for a user defined function which will call a message sub routine using the application.ontime method . I am passing arguments to the function that will execute at a certain time taken from selected cell . But my function doesnt work . I dont understand why. Can anyone help me with the codes and explain why my code dont work.

Function alarm (selectedcell as range)
sc=selectedcell.row
application.ontime Timevalue( cells(sc,1).text)," message"
End function

Sub message()
Msgbox " tested"
end sub
 
Errata....
The OnTime statement in the UDF does not return an error(!). But in fact, the event is not scheduled. Test it.

Actually, it is scheduled. It is just that when the event arrives, it does not call the event procedure.

IMHO, this is not due to the rule that UDFs cannot change the Excel environment.

In general, a VBA subroutine can be called from in a UDF context (i.e. from a VBA function that is called from an Excel worksheet directly or indirectly). But VBA does not permit subroutines in that context to change the Excel environment.

The same VBA subroutine can change the Excel environment if it is not called in a UDF context (i.e. from a VBA subroutine or function that is not called from an Excel worksheet directly or indirectly).

It is simply a context state.

With OnTime, the call to the VBA subroutine is simply deferred. There is no conceptual reason why VBA could not remember that it was scheduled in a UDF context and allow it to execute in general, but simply not permit it to change the Excel environment, just as it does when the UDF context is entered directly. SMOP!

Only a MSFT engineer could tell us if this is a design oversight (maybe even something MSFT might fix in the future; yeah, it our dreams!), or if was a conscious design decision and why.
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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