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
 
Hi Hot Pepper,

Bingo! It works perfectly now. Thank you to you and the other forum members for your help ... really appreciated. I have often referred to the Mr Excel forum for my Excel queries - and really happy you have helped me solve a problem that has been perplexing me for a while.

Wishing you a good rest of day.

Thanks again!

Shirley
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,224,521
Messages
6,179,289
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