I am trying to get a count of worksheets in the active workbook. When I run Application.Sheets.Count
the value I get is one off from what I expected I should be getting 76 and I get 75. In the project explorer I found a strange sheet labeled ThisWorkbook (See Sheet 52 in the image below). I would really like to understand this behavior as it has an impact on my later work. Any insight would be greatly appreciated.
Private Sub UserForm_Initialize()
Dim wsCnt As Long
wsCnt = Application.Sheets.Count
MsgBox wsCnt
End Sub
Edit: In light of some of the contributions below, I offer the following to further clarify what I'm seeing and to provide greater insight into why it is important within this context.
The above image is now a more complete picture of what I'm seeing. Note that ThisWorkbook
is at the bottom of the Project Explorer window. For some reason however, sheet 52 is also called ThisWorkbook
.
This is contextually important because this value is later used to increment through all created sheets, the number of which will change over time. Since Sheet52
increments the sheet count but not the number of sheets, I'm coming up short by one. I could conceivably introduce a workaround by just adding one, then checking to ensure that the sheet value is not equal to 52 on each iteration of the loop but in addition to being an inelegant approach this may also cause issues in the future if the anomaly crops up again. I would really like to understand this behavior. I haven't found anything in the documentation to indicate that this sheet needs to be created and I haven't seen a ThisWorkbook
sheet being created before like the case of Sheet52 nor can I fathom any reason why it would need to be created. I defer to the collective wisdom of the community.
Edit 2: Concerning the duplicate question
The suggestion that this question is a duplicate emanates from my curiosity as to what was happening to generate Sheet52
. The question however, pertains to getting the wrong sheet count, which is not addressed in that post. This information was instructive in some regards and did ultimately get me closer to the answer.