Nr of times a macro is run from a sheet

jb007

New Member
Joined
May 31, 2013
Messages
22
Hello everybody

I need to count how many times a macro is run from a specific worksheet.
For example if I am in sheet A and I run the macro 3 times, it ok to get the value 3, but if I move to sheet B and run it for the first time, I should get the value 1. Then if I move back and run from sheet A, value 4 should appear.

Using a static variable like the one from below that holds its value until the worbook is closed doesn't get the job done, because it counts every macro run, no matter from where.

Static a As Long
a = a + 1
 
The similar idea about usage of sheet's name was in post #7 but with a lot of my typos in comments and without clear explanations.
I prefer the Mike's detailed suggestion anyway – nice code & description as usual! :)
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Code:
   ....
    ....
    recordSheet.Range("A:B").ClearContents
    For Each oneBook In Application.Workbooks
        For Each oneSheet In oneBook.Worksheets
            With recordSheet.Range("A65536").End(xlUp).Offset(1, 0)
                .Cells(1, 1).Value = "[" & oneBook.Name & "] " & oneSheet.Name
                [B][COLOR=#FF0000]On Error Resume Next[/COLOR][/B]
                .Cells(1, 2).Value = Evaluate(oneSheet.Names("MacroRunCount").RefersTo)
            End With
        Next oneSheet
    Next oneBook
    ....
    ....
Why not move the red highlighted code line to right before the first For..Each statement so that it is not continually being executed with each iteration of the loops?
 
Upvote 0

Forum statistics

Threads
1,214,959
Messages
6,122,476
Members
449,087
Latest member
RExcelSearch

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