Give a cell as reference in a module

abdul43

Board Regular
Joined
Aug 19, 2011
Messages
58
HI ,
I have a code for getting increasing numbers in a cell.... that is .....
Code:
Sub Count()
With ActiveSheet.Range("H7")
.Value = .Value + 1
Application.Wait (Now + TimeValue("00:00:01"))
ActiveSheet.PrintOut
If .Value >= 10 Then Exit Sub 'limit
End With
Application.OnTime Now + TimeValue("00:00:02"), "Count" 'every 2 seconds
End Sub
here in line 6 "If .Value >= 10 Then Exit Sub 'limit" i want to replace the value 10 by a cell reference in a sheet within the same work book so it can take the value " limit" from a cell.
Advance thanks.
 
Last edited by a moderator:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Thanks now i have two modules first one is for starting macro (Code 1) & second one is for stoping the current macro (Code 2) these both codes are assigned to two different buttons the first one named as "Start" and the second as "Stop" now it start and runs correctly as i need but when the stop button is pressd it goes to vb and says:
" Compile error
Expacted function or variable"
will u give me some more time plz...
 
Last edited:
Upvote 0
Thanks now i have two modules first one is for starting macro (Code 1) & second one is for stoping the current macro (Code 2) these both codes are assigned to two different buttons the first one named as "Start" and the second as "Stop" now it start and runs correctly as i need but when the stop button is pressd it goes to vb and says:
" Compile error
Expacted function or variable"
will u give me some more time plz...

Apparently, the code I posted was wrong, I thought i had edited it, the second one should be
Code:
Sub stopthecode()
    stopcode = True
End Sub

and I hope you have a line like
Code:
Public stopcode as boolean
at the top of the module, you can put the two codes in the same module
 
Upvote 0
Now my cone in one module but the stop not working sir,
Rich (BB code):
Public stopcode As Boolean


Sub Count()
    stopcode = False
    If stopcode = False Then
        With ActiveSheet.Range("H7")
            Application.Wait (Now + TimeValue("00:00:01"))
            ActiveSheet.PrintOut
            .Value = .Value + 1
        If .Value > Worksheets("First-14").Range("A62").Value Then Exit Sub
        End With
        Application.OnTime Now + TimeValue("00:00:02"), "Count" 'every 2 seconds
    Else
        Exit Sub
    End If
End Sub
Sub stopthecode()
    stopcode = True
End Sub
is there any syntax error bcz i have changed the the sequence of print and wait in the loop.
 
Last edited by a moderator:
Upvote 0
Hi Abdul,

Does it give you any errors? or it doesnt just work?
 
Upvote 0
Remove the second line:
Code:
stopcode = False
 
Upvote 0
thanks RoryA it stopped the code but when i wanted to start it again using "code 1" then it was failed to start again. is this line is not compulsory for raising the flag to false for next time? although it dosent work.
 
Upvote 0
I have also tried add the following line somewhere

If stopcode = True Then Exit Sub

but where, faild to decide bcz when this goes into "with" it do not comes out before completing it.
 
Upvote 0
Amend the code slightly:
Code:
Public stopcode As Boolean

Sub Count()
    If stopcode Then
        stopcode = False
    Else
        With ActiveSheet.Range("H7")
            Application.Wait (Now + TimeValue("00:00:01"))
            ActiveSheet.PrintOut
            .Value = .Value + 1
        If .Value > Worksheets("First-14").Range("A62").Value Then Exit Sub
        End With
        Application.OnTime Now + TimeValue("00:00:02"), "Count" 'every 2 seconds

    End If
End Sub
Sub stopthecode()
    stopcode = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,662
Members
449,462
Latest member
Chislobog

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