A loop counter

JLouis

Active Member
Joined
Jan 1, 2004
Messages
295
Office Version
  1. 365
Platform
  1. Windows
Hello.

I am working on a workbook that uses random numbers that loops until a certain condition is met; a specific number.

I am wanting to be able to count how many times the loop has to cycle until the number is calculated correctly. Essentially a counter of the cycles.

Any help is appreciated.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
What kind of loop are you using?

Just insert a variable and increment it within your loop.
So, somewhere within your loop, you might have something like:
Code:
ctr = ctr + 1
To see how many times it ran, after the loop ends, you can simply return a message box, i.e.
Code:
MsgBox "The loop ran " & ctr & " times"

Here is a real simple example:
Code:
Sub Test()

    Dim i As Long
    Dim ctr As Long
    
    For i = 1 To 100
        ctr = ctr + 1
        If i > 14 Then Exit For
    Next i
        
    MsgBox "The loop ran " & ctr & " times"

End Sub
 
Last edited:
Upvote 0
Thank you for the quick reply & solution. It worked perfectly. I knew it was something simple....

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,731
Messages
6,180,611
Members
452,991
Latest member
JM_000888

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