excel 删除基于单元格值的工作表,但删除的工作表不同于活动工作表(VBA)

eqoofvh9  于 2023-03-24  发布在  其他
关注(0)|答案(2)|浏览(147)

我有一个充满数据的表,我有多个工作表,它们的名称与该表中B列中的单元格完全相同。如果I列中的值等于手动输入的值“A”,“B”,“C”,或“D”。如果同一行的第I列中的值等于这4个字母中的任何一个,我如何删除基于B列中找到的名称的工作表?
我只能找到删除行的方法,而且我是VBA的新手,所以我不知道从哪里开始

up9lanfz

up9lanfz1#

尝试

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
epggiuax

epggiuax2#

假设您在名为Main的工作表中有此表

+---+---+--------+---+---+---+---+---+---+---+
| . | A |   B    | C | D | E | F | G | H | I |
+---+---+--------+---+---+---+---+---+---+---+
| 1 |   | Apple  |   |   |   |   |   |   | A |
| 2 |   | Sheet2 |   |   |   |   |   |   | B |
| 3 |   | Tree   |   |   |   |   |   |   | C |
| 4 |   | Dog    |   |   |   |   |   |   | D |
| 5 |   | Bird   |   |   |   |   |   |   | A |
| 6 |   | Sheet6 |   |   |   |   |   |   | A |
+---+---+--------+---+---+---+---+---+---+---+

此外,工作簿中还有工作表,其名称为:

Sheet1
Sheet2
Sheet3
Sheet4
Sheet5
Sheet6
Main

因此,您要删除名为Sheet2Sheet6的工作表,因为它们的名称出现在列B中,并且在列I中,A, B, C, D之间还出现了一个字母
请记住,每个字母都有一个值(ASCII码),这个小表有你需要的每个字母的值。

+---+----+
| A | 65 |
| B | 66 |
| C | 67 |
| D | 68 |
+---+----+

请检查:https://www.ascii-code.com/
现在将以下代码放入一个普通模块:

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

相关问题