【问题标题】:Deleting rows from Excel, with the deletion rule changing based on the value in column A从 Excel 中删除行,删除规则根据 A 列中的值而变化
【发布时间】:2022-12-09 07:01:30
【问题描述】:

我是一名编码菜鸟,并试图根据列 H 中的值删除 Excel 工作表中的行,具体取决于列 A 的值是什么。例如,A列为“A”,如果H列为“Z”或“Y”或“X”,则删除行;如果 A 列是“B”,如果 H 列是“X”或“W”或“V”等,则删除行。

我知道如何从理论上做到这一点,但语法有问题。我的目的是让 A 列语句引用单元格值,B 列语句将单元格值与包含应删除的值的数组列表进行比较,如果值在列表中则删除该行。任何正确方向的帮助或指示将不胜感激。

到目前为止我的代码(如果我只是根据 A 列的值删除行,那效果很好)如下所示:

Dim LastRow As Long
Dim rowNum As Integer
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For rowNum = LastRow To 1 Step -1
    If (Range("A" & rowNum).Value = "A" And Range("H" & rowNum).Value = [reference to A list]) _
 Or (Range("A" & rowNum).Value = "B" And Range ("H" & rowNum).Value = [reference to B list]) Then
        Rows(rowNum).Delete
    End If
Next rowNum

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    在多列中有多个条件时删除行

    Sub DeleteMultiMatchingRows()
    
        Const COLS_LIST As String = "A,H"
        Const CRITS_LIST As String = "A;X,Y,Z|B;U,V,W,X,Y|C;W,X"
    
        Dim Cols() As String: Cols = Split(COLS_LIST, ",")
        Dim Crits() As String: Crits = Split(CRITS_LIST, "|")
        
        Dim nUpper As Long: nUpper = UBound(Crits)
        Dim cJag() As Variant: ReDim cJag(0 To nUpper)
        Dim cArr() As Variant: ReDim cArr(0 To 1)
        
        Dim SplitCrits() As String, n As Long
        
        For n = 0 To nUpper
            cJag(n) = cArr
            SplitCrits = Split(Crits(n), ";")
            cJag(n)(0) = SplitCrits(0)
            cJag(n)(1) = Split(SplitCrits(1), ",")
        Next n
        
        Erase SplitCrits
        Erase cArr
        Erase Crits
            
        Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
        Dim LastRow As Long: LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
        Dim r As Long, EqualString As String, MatchString As String
    
        For r = LastRow To 2 Step -1
            For n = 0 To nUpper
                EqualString = CStr(ws.Cells(r, Cols(0)).Value)
                If StrComp(EqualString, cJag(n)(0), vbTextCompare) = 0 Then
                    MatchString = CStr(ws.Cells(r, Cols(1)).Value)
                    If IsNumeric(Application.Match(MatchString, cJag(n)(1), 0)) Then
                        'Debug.Print r, EqualString, MatchString, ws.Rows(r).Address
                        'ws.Rows(r).Interior.Color = vbYellow
                        ws.Rows(r).Delete
                        Exit For
                    End If
                End If
            Next n
        Next r
    
    End Sub
    

    即时窗口结果

     21           C             X             $21:$21
     20           C             X             $20:$20
     18           C             W             $18:$18
     16           C             X             $16:$16
     15           B             V             $15:$15
     13           C             W             $13:$13
     11           A             X             $11:$11
     7            B             Y             $7:$7
     5            A             X             $5:$5
     2            B             Y             $2:$2
    

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 2015-11-14
      • 1970-01-01
      • 2016-10-25
      相关资源
      最近更新 更多