PDA

View Full Version : [SOLVED:] Error: Overflow



jejeserafico
03-11-2014, 01:09 PM
i'm having a problem regarding my code, i think the reason of overflow is the timer but i'm not sure of it

here is my code:


'my code for autostamp
Private Sub CommandButton2_Click()
Static x As Long
Dim t As Double
On Error GoTo exit_
Application.EnableCancelKey = xlErrorHandler
Do
x = x + 1
If Worksheets("STAMP2").Range("A" & x).Value = "" Then
MsgBox "STAMP COMPLETE!": Exit Do
Else
If cmbActivity.Text = "" Then Exit Do
txtRef.Text = Worksheets("STAMP2").Range("A" & x).Value
Worksheets("STAMP2").Range("A" & x).Interior.ColorIndex = 8
TIMESTAMP

t = Timer + 1
Application.Wait Now + TimeSerial(0, 0, 2)

If Timer < t Then Exit Do
End If
Loop
exit_:
End Sub

'my code for stopping the autostamp
'i did a keydown because my autostamp is not stopping using another button so i decided to make a keydown
Private Sub CommandButton2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 27 Then
MsgBox "STAMP CANCELED!"
End If
End Sub


is there other way to change my codes?

SamT
03-11-2014, 06:21 PM
I don't think Timer will ever be less than t


t = Timer + 1 't = timer + 1 second
Application.Wait Now + TimeSerial(0, 0, 2) 'wait 2 seconds
If Timer < t Then Exit Do ' If Timer + 2 less than timer + ]1

Application.Wait Now + TimeSerial(0, 0, 2) 'wait 2 seconds
Exit Do

jejeserafico
03-11-2014, 11:31 PM
I don't think Timer will ever be less than t


t = Timer + 1 't = timer + 1 second
Application.Wait Now + TimeSerial(0, 0, 2) 'wait 2 seconds
If Timer < t Then Exit Do ' If Timer + 2 less than timer + ]1

Application.Wait Now + TimeSerial(0, 0, 2) 'wait 2 seconds
Exit Do

thank you for that sir, i removed the exit do you added in able not to exit the loop, but is it the reason why it overflows?

or do you have any code or logic to provide for this kind of automation, thank you for replying sir!

SamT
03-12-2014, 08:23 AM
I am not sure what you mean by "Overflow."

In your original code, "IF Timer < t" will never be true, therefore the loop won't stop until it reaches the first empty cell in Column "A" unless cmbActivity.Text = "".

Examine this equivalent timer code
t1 = Timer + 1
Application.Wait Now + TimeSerial(0, 0, 2) '[Wait 2 seconds]
t2 = Timer
If t2 < t1 Then Exit Do

I believe that your entire loop can be rewritten to


With Worksheets("STAMP2").Range("A" & x)
If Not .Value = "" And Not cmbActivity.Text = "" Then
txtRef.Text = .Value
.Interior.ColorIndex = 8
TIMESTAMP
Application.Wait Now + TimeSerial(0, 0, 2)
Else
Msgbox "Done"
End If
End With

My personal preference would be
With Worksheets("STAMP2").Range("A" & x)
If .Value = "" Or cmbActivity.Text = "" Then Exit Sub

txtRef.Text = .Value
.Interior.ColorIndex = 8
TIMESTAMP
Application.Wait Now + TimeSerial(0, 0, 2)
End WithAnd I might change even that if I knew what the sub TIMESTAMP looked like.

Paul_Hossler
03-12-2014, 02:21 PM
i'm having a problem regarding my code,


In addition to SamT's advice, what is the problem ... specifically?

Doesn't run
Doesn't stop
Doesn't get the right answer?
etc.

Paul

jejeserafico
03-12-2014, 06:15 PM
i already realized it hehe

In your original code, "IF Timer < t" will never be true, therefore the loop won't stop until it reaches the first empty cell in Column "A" unless cmbActivity.Text = "".

i'll try to do this and hoping to work smoothly

With Worksheets("STAMP2").Range("A" & x)
If .Value = "" Or cmbActivity.Text = "" Then Exit Sub

txtRef.Text = .Value
.Interior.ColorIndex = 8
TIMESTAMP
Application.Wait Now + TimeSerial(0, 0, 2)
End With


i'm sorry but i can't show the sub TIMESTAMP to you

And I might change even that if I knew what the sub TIMESTAMP looked like

jejeserafico
03-12-2014, 06:16 PM
In addition to SamT's advice, what is the problem ... specifically?

Doesn't run
Doesn't stop
Doesn't get the right answer?
etc.

Paul

im having an overflow with my added codes

Jan Karel Pieterse
03-13-2014, 05:19 AM
On which line *exactly* do you get that overflow error?

jejeserafico
03-14-2014, 07:33 AM
i think it's now working well, thanks for your help in revising my code SAMT :clap2: