简体   繁体   中英

Hide/Unhide sheets - VBA

I have a worksheet containing 15 different sheets. I want a "main menu" sheet from where I can just click a button and then be redirected to the chosen sheet, while keeping all the other sheets hidden. From the chosen sheet I want to be able to go back to the "main menu" sheet again and still keep everything hidden. Thus, only one sheet will show at the time.

My problem is the following; The VBA code that I use is working perfectly, but sometimes it shows the error Run time error '1004': Method 'Visible' of object '_Worksheet'failed , and when I go to debug it shows the following debug screen .

As mentioned the code works "perfectly", but sometimes the run time error 1004 pops up. What am I doing wrong? The code is the following;

Sub Button8_Click()
Sheet3.Visible = xlSheetVisible
Dim sh As Worksheet

    For Each sh In ThisWorkbook.Sheets
        If sh.Name <> "practice group - mtd" Then
            sh.Visible = xlSheetHidden
        End If
    Next sh
End Sub

A faster workaround to avoid looping all sheets: keep the name of the visible sheet somewhere hidden in main menu sheet. (in a hidden cell, with FIXED location). Upon button click, if selection different than visible one, unhide selected, hide previous sheet, update main menu.

Sheets(“您的工作表名称”).Visible = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM