VBA: How to Loop Through Worksheets and Sum Values While Skipping Hidden Sheets?
I've searched everywhere and can't find a clear answer. I'm currently working on a VBA script that loops through all the worksheets in an Excel workbook to sum values from a specific range (A1:A10). However, I need to ensure that the script skips any hidden sheets. I've tried using the `Visible` property of the `Worksheet` object, but I'm encountering unexpected behavior. Some hidden sheets are still being included in the sum, which leads to incorrect results. Hereβs a simplified version of my code: ```vba Dim ws As Worksheet Dim total As Double total = 0 For Each ws In ThisWorkbook.Worksheets If ws.Visible = xlSheetVisible Then total = total + Application.WorksheetFunction.Sum(ws.Range("A1:A10")) End If Next ws MsgBox "Total Sum: " & total ``` When I run this code, I get a total that includes values from sheets I have hidden. I've double-checked that these sheets are indeed set to hidden and not very hidden, yet they still seem to contribute to the total. Is there something I'm missing, or is there a better way to handle this? Any advice would be greatly appreciated! Also, I'm using Excel 2016 for reference. My development environment is macOS. Has anyone else encountered this? My development environment is Windows.