通过检查工作簿中的工作表名来关闭userform

时间:2013-01-29 17:19:30

标签: excel-vba vba excel

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

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

1 个答案:

答案 0 :(得分: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