Macros in personal will not count sheets

Ron512

Board Regular
Joined
Nov 17, 2002
Messages
98
The below simple code run from personal will return a value of 1 regardless of the number of sheets in the workbook. The same macro copied to a module in a workbook returns the correct number of sheets.

Additionally all of the macros in my personal folder that use sheet counting code do not work, such as:
For Each WS In ThisWorkbook.Worksheets
ThisWorkbook.Worksheets.count

This is a new issue they have worked in the past. The personal folder is opening in hidden view and the workbook is not in edit mode.

What could have changed to create this issue?

Your assistance with correcting this is very much appreciated.

Ron


Code:
Sub getSheetCount()
    MsgBox ThisWorkbook.Worksheets.count
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try using activeworkbook rather than this workbook, this workbook refers to where the code is
 
Upvote 0
Thanks Dryver14, that was the fix and I think I understand why.
ThisWorkbook references the workbook in which the vba is executing.

So if I am executing code that is stored in personal on a workbook ActiveWorkbook should be used and if I’m executing code that resided in the workbook use ThisWorkbook.
Do I have that correct?
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,281
Members
449,149
Latest member
mwdbActuary

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