Sub DeleteSheet()
Dim lastRow As Long
Dim i As Long
lastRow = ActiveSheet.Cells(Rows.Count, "I").End(xlUp).Row
For i = 1 To lastRow
If ActiveSheet.Cells(i, "I") = ActiveSheet.Cells(i, "A") _
Or ActiveSheet.Cells(i, "I") = ActiveSheet.Cells(i, "B") _
Or ActiveSheet.Cells(i, "I") = ActiveSheet.Cells(i, "C") _
Or ActiveSheet.Cells(i, "I") = ActiveSheet.Cells(i, "D") Then
If ActiveSheet.Cells(i, "B") <> ActiveSheet.Name Then
Application.DisplayAlerts = False
'delete the sheet with the name in column B
ThisWorkbook.Worksheets(ActiveSheet.Cells(i, "B")).Delete
Application.DisplayAlerts = True
End If
End If
Next i
End Sub
Sub Delete_Sheets()
'Store the whole workbook
Dim Wrk As Workbook: Set Wrk = ThisWorkbook
'Store the main sheet where you have your table (I guess so...)
Dim Main As Worksheet: Set Main = Wrk.Worksheets("Main")
Dim Sht As Worksheet
Dim i As Range
Dim ShtNames As Range
Dim RngLetter As Range
Dim StrLetter As String
Dim NumLetter As Integer
'Find the last row in column B, and also (guess) the table begins in row 2
Dim LRow: LRow = Main.Range(Main.Cells(Rows.Count, "B"), Main.Cells(Rows.Count, "B")).End(xlUp).Row
Dim iR
'Set the range from column B to the last cell
Set ShtNames = Main.Range(Main.Cells(2, "B"), Main.Cells(LRow, "B"))
'Let's loop!
For Each i In ShtNames
'Get the row of the cell in column B
iR = i.Row
'Get the cell in column I in the same row.
Set RngLetter = Main.Cells(iR, "I")
'Set the letter to capital from the value. We need A instead of a
StrLetter = UCase(RngLetter.Value)
'Take the ASCII value of the letter A=65 B=66 C=67 and so on.
NumLetter = Asc(StrLetter)
'Another loop...
'Now with the value in the cells from columns B and I, let's check.
For Each Sht In Wrk.Worksheets
'If the name of the sheet is equal to the cell in column B
'and NumLetter is between 65 and 68
'Remember ASC("A") returns 65
If Sht.Name = i.Value And NumLetter >= Asc("A") And NumLetter <= Asc("D") Then
'Don't show the alerts!
Application.DisplayAlerts = False
'Delete the sheet!
Sht.Delete
'Turn on the alerts
Application.DisplayAlerts = True
End If
Next Sht
Next i
End Sub
2条答案
按热度按时间up9lanfz1#
尝试
epggiuax2#
假设您在名为
Main
的工作表中有此表此外,工作簿中还有工作表,其名称为:
因此,您要删除名为
Sheet2
和Sheet6
的工作表,因为它们的名称出现在列B
中,并且在列I
中,A, B, C, D
之间还出现了一个字母请记住,每个字母都有一个值(ASCII码),这个小表有你需要的每个字母的值。
请检查:https://www.ascii-code.com/
现在将以下代码放入一个普通模块: