假設我有 3 個作業表“Asutosh”、“Asutosh2”、“Asutosh3”,我想使用 vba 洗掉“Asutosh2”和“Asutosh3”。
我使用了 vba 但我必須手動為其他名稱執行此操作,例如如果我為 Asutosh 錄制,其他額外的重復作業表不會洗掉。
uj5u.com熱心網友回復:
您可以使用兩種方法。
如果您希望擁有一個不會洗掉但其他所有作業表都會洗掉的主作業表,請使用類似于以下內容的內容。
Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Closes all other worksheets par "Asutosh", saves user time not having to delete imported sheets everytime
For Each ws In ThisWorkbook.Worksheets
Application.DisplayAlerts = False
If ws.Name <> "Asutosh" Then 'If a worksheet is not named "Asutosh" it gets deleted
ws.Delete
End If
Next ws
Application.DisplayAlerts = True
MsgBox ("All sheets are deleted except specific sheet - After this, you can click either 'Save' or 'Don't Save' button") 'Message box to reasure user is okay with either option when closing the file
End Sub
在 VBA 編輯器中將此 ^ 添加到 ThisWorkbook
或者,如果您希望洗掉具有特定名稱的作業表,請使用以下命令
Sub vba_delete_sheet()
Sheets("Asutosh2").Delete
Sheets("Asutosh3").Delete
End Sub
希望這可以幫助!
uj5u.com熱心網友回復:
而這樣的方式是可能的
Sub DelDubl()
Dim wsh As Worksheet, j As Long, wshNm
' array with names of worksheets which are not to delete
wshNm = Array("Asutosh")
For Each wsh In ThisWorkbook.Worksheets
For j = LBound(wshNm) To UBound(wshNm)
If wsh.Name Like wshNm(j) Then GoTo NXT1
Next j
Application.DisplayAlerts = False
wsh.Delete
Application.DisplayAlerts = True
NXT1:
Next
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521991.html
標籤:擅长vba
上一篇:在任務項中設定附件的位置在Outlook2019中不起作用
下一篇:將標題添加到每條記錄作為行
