根据主表上的单元格值隐藏工作表时出错

时间:2019-06-22 07:20:17

标签: excel vba hide

我正在尝试学习宏,并通过给出yes或no来构建代码以隐藏特定工作表。使用了以下代码,但出现脚本错误

在主工作表中,我已经在Col H中提到了所有工作表的名称(从第5行开始),并提到了“是”或“否”,以使其可见或隐藏。

Sub Button2_Click()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Master")
Dim i

Application.ScreenUpdating = False
    For i = 4 To ws.Range("I" & ws.Rows.Count).End(xlUp).Row
        If ws.Range("I" & i) = "Yes" Then
            ThisWorkbook.Sheets(i - 2).Visible = xlSheetVisible
        ElseIf ws.Range("I" & i) = "No" Then
            ThisWorkbook.Sheets(i - 2).Visible = xlSheetHidden
            End If
            Next i
Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

这将起作用:

Sub Button2_Click()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Master")
Dim i As Integer

Application.ScreenUpdating = False

    For i = 5 To ws.Range("I" & ws.Rows.Count).End(xlUp).Row

        If ws.Range("I" & i) = "Yes" Then
            ThisWorkbook.Worksheets(ws.Range("H" & i).Value).Visible = xlSheetVisible
        ElseIf ws.Range("I" & i) = "No" Then
            ThisWorkbook.Worksheets(ws.Range("H" & i).Value).Visible = xlSheetHidden
        End If

    Next i

Application.ScreenUpdating = True

End Sub