Click here to Skip to main content
15,895,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
OK, this is my predicament :

If I run my VBA program I get an error message "Argument Not Optional" ... but if I debug I get the message "Can't execute code in break mode" !!

My main VBA is in Thisworkbook and the new code is as follows :

VB
Range("B11").Activate
    
    Set myRange1 = Range("B" & (10 + mySession))
    Set myRange2 = Range("H" & (10 + mySession))
    
    If myRange1 = "" Then
        Call StartBlink(myRange2)
    End If
        
    Application.ScreenUpdating = True


The Module Blinking_Text contains the following code (this is adapted from code I have found online, I am not sure what this RunWhen stuff does) :

VB
Option Explicit
Public RunWhen As Double
 
Sub StartBlink(BlinkCell As Range)
    
    If BlinkCell.Font.ColorIndex = 1 Then
        BlinkCell.Font.ColorIndex = 2
    Else
        BlinkCell.Font.ColorIndex = 1
    End If
    
    RunWhen = Now + TimeSerial(0, 0, 1)
    
    Application.OnTime RunWhen, "StartBlink", , True
    
End Sub
 
Sub StopBlink(BlinkCell As Range)

    BlinkCell.Font.ColorIndex = xlAutomatic
    
    Application.OnTime RunWhen, "StartBlink", , False
    
End Sub


Any ideas ?
Posted

1 solution

You are getting the "Argument not optional" message because the StartBlink sub-routine expects a parameter to be passed to it and you have not included the BlinkCell in the call to OnTime

Normally, to pass a parameter using OnTime you would enclose the sub name and parameter list within single-quotes, such as
Application.OnTime RunWhen, "'StartBlink H,10'", , True
but that is not an easy thing when the parameter is a Range. You could pass in the cell name and construct the range within the StartBlink sub, but I found it easier just to pull the range out to a public variable
Public BlinkCell As Range
so the routine signature becomes
Sub StartBlink()

That now works.
Gary Heath wrote:
I am not sure what this RunWhen stuff does
Chip Pearson wrote the original code and in this article[^] he explains why RunWhen is abstracted out to a public variable - see the section on stopping the timer.
Gary Heath wrote:
if I debug I get the message "Can't execute code in break mode" !!
That message is generated from the pending OnTime call. Basically it is saying that it can't execute the call because you have the program stopped in the debugger and it is trying to run it every second. If you temporarily change the interval e.g.
RunWhen = Now + TimeSerial(0, 0, 15)
you will get an opportunity (well, a 15 second opportunity!) to debug. Alternatively you can call the StopBlink routine from the Immediate window to cancel any pending timers.
 
Share this answer
 
Comments
Gary Heath 26-Mar-15 12:36pm    
Brilliant, thank you :-)
CHill60 26-Mar-15 12:50pm    
My pleasure!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900