簡體   English   中英

object '_Worksheet' 的方法 'Visible' 失敗

[英]Method 'Visible' of object '_Worksheet' failed

不知道為什么這會失敗,但無論出於何種原因,當 ActiveSheet 是控制台時它會失敗。 反過來,它工作正常。 代碼如下。

    Sub Switch_Books()
    Dim ws As Worksheet
    protect_book True

    If ActiveSheet.Name = "Console" Then
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = "CDA Console" Then
                ws.Visible = xlSheetVisible
            Else
                ws.Visible = xlSheetHidden
            End If
        Next ws
    Else
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = "Console" Then
                ws.Visible = xlSheetVisible
            Else
                ws.Visible = xlSheetHidden
            End If
        Next ws
    End If

    protect_book False


    End Sub

嘗試這個:

    Sub Switch_Books()
        Dim ws As Worksheet, wsName
        
        protect_book True 'This is a confusing call...
                          ' you should switch the way the boolean works 
    
        wsName = IIf(ActiveSheet.Name = "Console", "CDA Console", "Console")
        ThisWorkbook.Sheets(wsName).Visible = xlSheetVisible 'must be at least one sheet visible
        
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name <> wsName Then ws.Visible = xlSheetHidden
        Next ws
        
        protect_book False
    
    End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM