-1

我正在尝试在多个工作表的工作簿中删除包含 ISIN 值的行。

我可以删除几张纸中的 ISIN,但不能删除校准表和燃料和质量表。

Sub del()
    Dim num As Integer
    Dim del As String
    Dim q As Integer
    Dim a As Integer
    Dim i As Integer

    num = Range("A" & Rows.Count).End(xlUp).Row
    del = InputBox("Enter ISIN", "ISIN")
    a = Application.Worksheets.Count

    For q = 1 To a
        Worksheets(q).Activate
        If ActiveSheet.Name = "Price Selection for Upload" Then
            For i = 1 To num
                If Range("A" & i).Value = del Then
                    ActiveSheet.Rows(i).EntireRow.Delete
                End If
            Next i
        ElseIf ActiveSheet.Name = "Main Sheet" Then
            For i = 1 To num
                If Range("A" & i).Value = del Then
                    ActiveSheet.Rows(i).EntireRow.Delete
                End If
            Next i
        ElseIf ActiveSheet.Name = "Spread Calibration" Then
            For i = 1 To num
                If Range("A" & i).Value = del Then
                    ActiveSheet.Rows(i).EntireRow.Delete
                End If
            Next i
        ElseIf ActiveSheet.Name = "Fuel& Quality" Then
            For i = 1 To num
                If Range("B" & i).Value = del Then
                    ActiveSheet.Rows(i).EntireRow.Delete
                End If
            Next i
        ElseIf ActiveSheet.Name = "Sheet3" Then
            Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
            ActiveCell.Value = del
        End If
    Next q

    Worksheets("Email Details").Activate
    Worksheets("Email Details").Range("Q2").Select

End Sub
4

1 回答 1

0

尝试这个。您可以通过组合具有相同结果的案例来缩短循环。我使用了 Select Case,但您可以坚持使用 If,关键是您不需要为每个工作表名称重复所有内容。

更重要的是,当向后删除循环时,您可能会跳过行。

此外,遍历每一行的速度很慢,因此请考虑使用Findor Autofilter

避免使用 Select/Activate

如果工作表名称未正确提取,请检查尾随或前导空格等。

Sub del()

Dim num As Long 'use Long rather than integer
Dim del As String
Dim q As Long
Dim a As Long
Dim i As Long

num = Range("A" & Rows.Count).End(xlUp).Row 'need to add sheet reference
del = InputBox("Enter ISIN", "ISIN")
a = Application.Worksheets.Count

For q = 1 To a
    With Worksheets(q)
        Select Case .Name
            Case "Price Selection for Upload", "Main Sheet", "Spread Calibration"
                For i = num To 1 Step -1 'loop backwards
                    If .Range("A" & i).Value = del Then
                        .Rows(i).EntireRow.Delete
                    End If
                Next i
            Case "Fuel& Quality"
                For i = num To 1 Step -1
                    If .Range("B" & i).Value = del Then
                        .Rows(i).EntireRow.Delete
                    End If
                Next i
            Case "Sheet3"
                .Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = del
        End Select
    End With
Next q

Application.Goto Worksheets("Email Details").Range("Q2")

End Sub
于 2020-02-18T09:45:10.673 回答