Delete specific rows from selection in for each loop

前端 未结 1 2009
慢半拍i
慢半拍i 2021-01-23 08:14

I\'m trying to delete rows which don\'t contain new words.

What I do:

  1. Select multiple rows manually
  2. Run macro, which checks ea
相关标签:
1条回答
  • 2021-01-23 08:57

    Always run from the bottom to the top when deleting rows or you risk skipping rows (as you have noticed).

    'don't dim inside a loop
    Dim r As Long
    Dim strArray As Variant
    Dim intCount As Integer
    Dim intWords As Integer
    
    With Selection
        For r = .Rows.Count To 1 Step -1
    
            strArray = Split(Selection.Cells(r).Value & " ", " ")
            intWords = 0
    
            For intCount = LBound(strArray) To UBound(strArray) - 1
                If dict.Exists(Trim(strArray(intCount))) Then
                    dict(Trim(strArray(intCount))) = dict(Trim(strArray(intCount))) + 1
                Else
                    dict.Add Key:=Trim(strArray(intCount)), Item:=1
                    intWords = intWords + 1
                End If
            Next intCount
    
            If intWords = 0 Then
                .Cells(r).EntireRow.Delete
            End If
    
        Next r
    End With
    
    0 讨论(0)
提交回复
热议问题