daveOverHere
New Member
- Joined
- Jul 30, 2015
- Messages
- 2
I am trying to simulate some logic within a spreadsheet in Excel.
All the basic logic gates are obvious but I am having trouble generating a pulse function.
My intention was to create a function that would set the value of the cell to 1 and then clear it down to 0 after a delay using the Application.OnTime method.
When I use the OnTime method within a sub all is well, but as soon as I try to call it from a function it seems to wander off and get lost.
An example of what works is:
Sub test()
Application.OnTime Now + TimeValue("00:00:02"), "clearPulseDown"
End Sub
Sub clearPulseDown()
MsgBox "yay!"
End Sub
If I run 'test', then 2 seconds later the message box appears.
However, if I create a function "Pulse" and use the function in a spreadsheet I cannot get the message box even though I know that the code is running as I see lastOP changing for the following code (don't worry that the logic for the pulse is not correct, I have trashed everything in a attempt to get the OnTime to work).
Function Pulse(IP As Range) As Integer
If IP = 1 Then
Application.OnTime Now() + TimeValue("00:00:02"), "clearPulseDown"
Pulse = 1
Else
Pulse = 0
End If
End Function
Sub clearPulseDown()
MsgBox "yay!"
End Sub
I tried the following to see that I could call a sub from a function ad it all works:
Function Pulse(IP As Range) As Integer
If IP = 1 Then
clearPulseDown
Pulse = 1
Else
Pulse = 0
End If
End Function
Sub clearPulseDown()
MsgBox "yay!"
End Sub
Is anyone able to point out where I am wrgoing wrong?
All the basic logic gates are obvious but I am having trouble generating a pulse function.
My intention was to create a function that would set the value of the cell to 1 and then clear it down to 0 after a delay using the Application.OnTime method.
When I use the OnTime method within a sub all is well, but as soon as I try to call it from a function it seems to wander off and get lost.
An example of what works is:
Sub test()
Application.OnTime Now + TimeValue("00:00:02"), "clearPulseDown"
End Sub
Sub clearPulseDown()
MsgBox "yay!"
End Sub
If I run 'test', then 2 seconds later the message box appears.
However, if I create a function "Pulse" and use the function in a spreadsheet I cannot get the message box even though I know that the code is running as I see lastOP changing for the following code (don't worry that the logic for the pulse is not correct, I have trashed everything in a attempt to get the OnTime to work).
Function Pulse(IP As Range) As Integer
If IP = 1 Then
Application.OnTime Now() + TimeValue("00:00:02"), "clearPulseDown"
Pulse = 1
Else
Pulse = 0
End If
End Function
Sub clearPulseDown()
MsgBox "yay!"
End Sub
I tried the following to see that I could call a sub from a function ad it all works:
Function Pulse(IP As Range) As Integer
If IP = 1 Then
clearPulseDown
Pulse = 1
Else
Pulse = 0
End If
End Function
Sub clearPulseDown()
MsgBox "yay!"
End Sub
Is anyone able to point out where I am wrgoing wrong?