How to create a counter in Excel VBA

shirleyj

New Member
Joined
Sep 14, 2011
Messages
37
Dear All,

I am fairly new to VBA and I am having trouble getting the below code to run correctly.

I am trying to create a counter that will increment from 0 up to the maximum number set in the cell named “selected”. So if the cell “selected” contains the number 10, the cell “counter” will increase from 0 to 1 to 2, etc until it reaches the maximum of 10 and ideally would then loop back to 1 again.

When I run the code in ‘step into’ mode, it works fine, but when I run the entire code, it jumps straight ahead to 9 and I cannot figure out why.

Any help would be really appreciated!

Thank you for your time.

Best regards,


Shirley

sub counterincrement()

Dim a As Integer
Dim b As Integer
Dim c As Integer

' "selected" is the cell showing number of visible rows in a filtered list (eg 10). The counter below should not exceed this number
' "counter" is the name of the cell which should increment from 0 up to maximum number in selected cell


a = Range("selected").Value
b = Range("counter").Value
c = 0

Do

Range("counter").Value = c
c = c + 1
Loop Until c = a




End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try this
Code:
Sub counterincrement()

Dim a As Integer
Dim c As Integer

' "selected" is the cell showing number of visible rows in a filtered list (eg 10). The counter below should not exceed this number
' "counter" is the name of the cell which should increment from 0 up to maximum number in selected cell


a = Range("selected").Value
c = 1

For c = 1 To a
    Range("counter").Value = c
Next c



End Sub
 
Upvote 0
Perhaps

Code:
Sub test()
    Dim limitCell As Range
    Dim countingCell As Range
    Set limitCell = Sheet1.Range("A1")
    Set countingCell = Sheet1.Range("B2")

    With countingCell
        .Value = 1 + (Val(CStr(.Value)) Mod (Val(CStr(limitCell.Value))))
    End With
End Sub
 
Upvote 0
Hi Yahya,

Thanks for your quick reply. I couldn't get this code to work either .... like in my code, it jumped straight ahead to the last item in the list, instead of incrementing through one by one. Do you know why this is happening?

Many thanks,

Shirley
 
Upvote 0
Is the code that I posted doing what you want. You need to call it every time you want the cell incrimented.
 
Upvote 0
It's not jumping ahead, it is just incrementing so fast you don't see it happening. What you need is a delay in the code, if you are wanting to see it increment. Keep in mind you are not going to be able to do anything else while the code is running:

Code:
Sub test()
Dim c As Range, d As Range
Set c = Range("selected")
Set d = Range("counter")
Do
    d = (d Mod c) + 1
    Application.Wait Now() + TimeValue("00:00:01")
Loop
End Sub
Hit CTRL-BREAK to end it.
 
Upvote 0
Hi Mikerickson,

Just posted you a response, but for some reason it was lost! I am trying again now.

Shirley
 
Upvote 0
Hi Mikerickson,

Thank you for your message – and apologies for the delay in responding. I have just been trying to get your code to work in my spreadsheet. I created a new workbook with your code and it worked perfectly. Now just having troubles making it work in mine. As I am quite new to VBA, this is probably a very basic question ….. is it possible to set the variables with the cell name, ie the variable “limitCell” is located in a cell I have called “selected” and the variable “countingCell” is in a cell I have called “counter”. I tried substituting sheet1.range(“A1”) with workings.range(“d1”) – the name & cell address on my spreadsheet, but this created an error.

Many thanks in advance for your assistance.

Best regards,

Shirley
 
Upvote 0
Hi HotPepper,

Thanks for your msg. Actually, as Mickerikson suggested, I do want to call it everytime to increment the cell. His code should work, once I understand how to set the variables to the cells I have named.

Thanks for your suggestion .... could still come in handy in another situation!

Best regards,

Shirley
 
Upvote 0
Then this should do it:

Code:
Sub test()
Dim c As Range, d As Range
Set c = Range("selected")
Set d = Range("counter")
d = (d Mod c) + 1
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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