我正在尝试学习宏,并通过给出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
答案 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