0

通过使用命令按钮,可以通过检查工作簿中是否存在特定工作表名称来关闭用户窗体

Private Sub Close1_Click()
' Protect and Hide

 Dim ws As Worksheet

    For Each ws In ThisWorkbook.Sheets
        If InStr(1, ws.Name, "FSS-TEM-00025") Then
            (ws.Name, "FSS-TEM-00025" ).Select
            ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
            ActiveWindow.SelectedSheets.Visible = False
            Unload Me
        Else
            Unload Me
        End If
    Next

    'Unload Me
End Sub
4

1 回答 1

0

这将起作用。在 If... 之后,您没有正确使用 ws 引用

Private Sub Close1_Click()
' Protect and Hide

Dim ws As Worksheet

For Each ws In ThisWorkbook.Sheets
    If InStr(1, ws.Name, "FSS-TEM-00025") Then
        ws.Select 'this line is different
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        ActiveWindow.SelectedSheets.Visible = False 'hides the activesheet
        Unload Me
    End If
Next

'Unload Me
End Sub

而不是选择然后使用活动表:

ws.Select 'this line is different            
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

只能是

ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
于 2013-01-29T21:41:09.103 回答