Workbook Worksheet If, Then, ElseIf not working to get worksheet visible property

ddander54

Board Regular
Joined
Oct 18, 2012
Messages
97
I have cobbled some code together from multiple places that creates a new workbook, then adds an Index sheet and loops thru a selected folder to get the Workbook name, Worksheet name and then the Worksheet.visible property. All of it is working fine except the Worksheet.visible property. It displays that a sheet is visible when it is hidden. I have tried If, Then, ElseIf, End If and multiple If, Then, End If and neither seems to work for me. Thoughts?
Results: File WorkMix.xlsx Sheet3 should read Hidden, The rest are Visible, which is correct.
C:\MyExcelFiles\Test2\*.xls*
Book1.xlsx
MainVisible
DataVisible
Data.xlsx
DataVisible
History.xlsx
HistoricalVisible
Source.xlsm
Sheet1Visible
Summary.xlsm
SummaryVisible
WorkMix.xlsx
ActualsVisible
Sheet1Visible
Sheet2Visible
Sheet3Visible <<<< This should be "Hidden"
---END OF FOLDER---

VBA Code:
'Option Explicit

'https://www.mrexcel.com/board/threads/loop-through-every-file-in-folder-and-list-all-sheet-names.663583/

Sub FolderCrawler()
FileType = "*.xls*"     'The file type to search for
FilePath = "C:\MyExcelFiles\Test2\"   'The folder to search
OutputRow = 2   'The first row of the active sheet to start writing to

Dim ws As Worksheet, NewSheet As Worksheet, wb As Workbook, I As Integer

Set wb = Workbooks.Add
Set NewSheet = Sheets.Add(After:=Sheets(Worksheets.Count), Count:=1, Type:=xlWorksheet)
NewSheet.name = "Index"

OutputRow = 2   'The first row of the active sheet to start writing to
wb.ActiveSheet.Range("A" & OutputRow) = FilePath & FileType
OutputRow = OutputRow + 1
Curr_File = Dir(FilePath & FileType)
Do Until Curr_File = ""
    Set FldrWkbk = Workbooks.Open(FilePath & Curr_File, False, True)
    wb.ActiveSheet.Range("A" & OutputRow) = Curr_File
    wb.ActiveSheet.Range("B" & OutputRow).ClearContents   'Clear any previous values
    OutputRow = OutputRow + 1
   
    For Each sht In FldrWkbk.Sheets
        wb.ActiveSheet.Range("B" & OutputRow) = sht.name
        If wb.ActiveSheet.Visible = xlSheetVisible Then
        wb.ActiveSheet.Range("C" & OutputRow) = "Visible"
        End If
        If wb.ActiveSheet.Visible = xlSheetHidden Then
        wb.ActiveSheet.Range("C" & OutputRow) = "Hidden"
        End If
        If wb.ActiveSheet.Visible = xlSheetVeryHidden Then
        wb.ActiveSheet.Range("C" & OutputRow) = "Very Hidden"
        End If
        'wb.ActiveSheet.Range("C" & OutputRow) = sht.Visible
        wb.ActiveSheet.Range("A" & OutputRow).ClearContents 'Clear any previous values
        OutputRow = OutputRow + 1
    Next sht
 
    FldrWkbk.Close SaveChanges:=False
    Curr_File = Dir
Loop
Set FldrWkbk = Nothing
wb.ActiveSheet.Range("A" & OutputRow) = "---END OF FOLDER---"
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Your If statements are checking the wrong worksheet. You are checking the sheet where you are writing your report, rather than the sheet in the workbook you want to check. Also use If/ElseIf instead of independent If because the conditions are mutually exclusive.

Tested.

Rich (BB code):
    For Each sht In FldrWkbk.Sheets
        wb.ActiveSheet.Range("B" & OutputRow) = sht.Name
        If sht.Visible = xlSheetVisible Then
         wb.ActiveSheet.Range("C" & OutputRow) = "Visible"
        ElseIf sht.Visible = xlSheetHidden Then
         wb.ActiveSheet.Range("C" & OutputRow) = "Hidden"
        ElseIf sht.Visible = xlSheetVeryHidden Then
         wb.ActiveSheet.Range("C" & OutputRow) = "Very Hidden"
        End If
        'wb.ActiveSheet.Range("C" & OutputRow) = sht.Visible
        wb.ActiveSheet.Range("A" & OutputRow).ClearContents 'Clear any previous values
        OutputRow = OutputRow + 1
    Next sht
 
Upvote 0
Solution
Your If statements are checking the wrong worksheet. You are checking the sheet where you are writing your report, rather than the sheet in the workbook you want to check. Also use If/ElseIf instead of independent If because the conditions are mutually exclusive.

Tested.

Rich (BB code):
    For Each sht In FldrWkbk.Sheets
        wb.ActiveSheet.Range("B" & OutputRow) = sht.Name
        If sht.Visible = xlSheetVisible Then
         wb.ActiveSheet.Range("C" & OutputRow) = "Visible"
        ElseIf sht.Visible = xlSheetHidden Then
         wb.ActiveSheet.Range("C" & OutputRow) = "Hidden"
        ElseIf sht.Visible = xlSheetVeryHidden Then
         wb.ActiveSheet.Range("C" & OutputRow) = "Very Hidden"
        End If
        'wb.ActiveSheet.Range("C" & OutputRow) = sht.Visible
        wb.ActiveSheet.Range("A" & OutputRow).ClearContents 'Clear any previous values
        OutputRow = OutputRow + 1
    Next sht
Doh!!! Guess it helps to look at the correct worksheet! Thanks for 'strumming' me in the right direction!
 
Upvote 0
Glad to help! :)

I didn't see it myself until I ran your code and reproduced the problem, then saw it right away.
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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