0

I currently have a code that acts as a countdown timer. The reason I'm not using a timer function or simply using the "CountUp" portion to countdown for me is because i'd like it to self adjust (another code could be running causing the calculation time for the other countdown methods to vary and be lengthy). Whenever the code reaches zero it causes an error: Application defined or object defined error.

 Sub CountupONE()
 Dim CountDownONE As Date

CountDownONE = Now + TimeValue("00:00:01")

Application.OnTime CountDownONE, "RealcountONE"
End Sub


Sub RealcountONE()
 Dim countONE As Date
 Dim counterONE As Date

 counterONE = Now - Sheets("Sheet1").Range("$A$11").Value
 countONE = Sheets("Sheet1").Range("$B$8").Value - counterONE
 'MsgBox countONE
 Sheets("Sheet1").Range("$C$8") = countONE
 'Sheets("Sheet1").[C8] = TimeValue("12:00:00 AM")

If countONE = TimeValue("12:00:00 AM") Then

Beep
[C11].Value = Now
[C11].NumberFormat = "h:mm:ss AM/PM"
Application.Speech.Speak ("Platen one is done")
Call BeginGraphing

Exit Sub

End If

Call CountupONE
End Sub
8
  • which line causes the error? Commented Feb 24, 2015 at 18:02
  • Sorry, it erros when it reachs: Sheets("Sheet1").Range("$C$8") = countONE Commented Feb 24, 2015 at 18:03
  • 1
    What's in B8 and A11? Can you debug.print that? Commented Feb 24, 2015 at 18:15
  • Application or object-defined error may indicate the worksheet is protected. Is the worksheet protected? Commented Feb 24, 2015 at 18:18
  • 2
    I think it's do with the way he's subtracting date datatypes that is causing the error. Commented Feb 24, 2015 at 18:23

2 Answers 2

1

This should resolve it:

 Sheets("Sheet1").Range("$C$8").Value = TimeValue(CStr(countONE))

I am not sure why... It is possible to put a raw Date into a cell: [A1].Value = CDate(Now), or even this does not raise an error:

countOne = Now()
Range("A1").Value = countOne

When the error raises in your case, the value of countOne is: March 5, 1785 [and some time value portion]. This has a Long equivalent of -41938. I don't know why this would cause an error and I would not expect it to.

In any case this method I suggest first coerces countOne into a String data type, and then pulls out the time portion of that value.

Sign up to request clarification or add additional context in comments.

1 Comment

I wish I knew why the original code was causing an error. I certainly would not expect it to :(
1

12:00:20 AM is not date data type in your case.

The content of A11 and B8 should in the form of mm/dd/yyyy for you code to pass. You can add time to that.

Try 02/27/2015 and it will go through!

Edit: you can get to work with time like the following line:

If Format(countONE, "hh:mm:ss AM/PM") = "12:00:00 AM"
'do here
End if

1 Comment

Nevermind, I think im understanding... I'll mess around with it for a few minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.