【问题标题】:Delete entire row if 3 columns is duplicate and 1 column is blank如果 3 列重复且 1 列为空白,则删除整行
【发布时间】:2021-08-17 15:27:44
【问题描述】:

我有 3 列。我需要根据第 1、2、3 列删除具有重复项的行。如果一行包含来自另一行的相同数据但第 2 列为空白,这也将被视为重复。删除第2行空白列,保留数据完整的行。

以黄色突出显示的被认为是重复的,必须删除第 2 列中没有数据的行。

我被困在这里了。

Sub DeleteRows()
    Dim Rng As Range
    Dim i As Long
    Set Rng = ThisWorkbook.ActiveSheet.Range("F:F")
    With Rng
       
        For i = .Rows.Count To 1 Step -1
            If .Item(i) = "" Then
                
                .Item(i).CurrentRegion.RemoveDuplicates Columns:=Array(1,3,)
            End If
        Next i
    End With
End Sub

提前谢谢你!

【问题讨论】:

  • if cells(i,6)="" and (cells(i,4)=cells(i,7)=cells(i,8)) then rows(i).entirerow.delete
  • (cells(i,4)=cells(i,7)=cells(i,8)) 在 VBA 中即使为真也返回假。 If worksheetfunction.And(cells(i,6)="",cells(i,4)=cells(i,7),cells(i,7)=cells(i,8)) Then 怎么样?
  • 谢谢,两者都很好,但副本仍然存在。我还需要进一步修改还是只复制粘贴条件?
  • 为了数据输入,会不会有前导空格或结尾空格?如果处理非数字值,您可能需要使用Trim() 进行比较,例如“Hi”的Trim(Cells(i,4).Value) 输出“Hi”。

标签: excel vba


【解决方案1】:

如果空白单元格删除重复项

  • 一些示例数据和/或屏幕截图可能会让更多人了解您的目标。
  • RemoveDuplicates 无法为所欲为。
  • 相关数据只是Blank Column6F)中单元格为空白(Empty=""',...)的范围内的行。
  • 重复是指在Duplicate Columns4, 7, 8D, G, H)的所有单元格中的前一行具有相同值的行。
Option Explicit

Sub DeleteDuplicatesIfBlank()
    
    Const dColsList As String = "D,G,H" ' Duplicate Columns List (2 at least)
    Const bCol As String = "F" ' Blank Column
    Const Delimiter As String = "||" ' Dictionary Delimiter
    
    ' Create a reference to the range ('rg').
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
    Dim rg As Range: Set rg = ws.Range("A1").CurrentRegion
    
    ' Write the Duplicate columns to the Duplate Columns Array ('dCols').
    Dim dCols() As String: dCols = Split(dColsList, ",")
    Dim dUpper As Long: dUpper = UBound(dCols)
    
    ' Define the Dictionary ('dict').
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare ' ignore case i.e. A=a
    
    ' Declare additional variables.
    Dim drg As Range ' Delete Range
    Dim rrg As Range ' Current Row Range
    Dim n As Long ' Duplicate Column Counter
    Dim cString As String ' Current String (created from the Duplicate Columns)
    
    ' Loop through each row ('rrg') of the range.
    For Each rrg In rg.Rows
        ' Only consider the row if its Blank Column is blank.
        If Len(CStr(rrg.Columns(bCol).Value)) = 0 Then
            ' Concatenate the strings from the Duplicate Columns to 'cString'.
            cString = CStr(rrg.Columns(dCols(0)).Value) ' first
            For n = 1 To dUpper ' remainder
                cString = cString & Delimiter & CStr(rrg.Columns(dCols(n)))
            Next n
            If dict.Exists(cString) Then ' duplicate found
                ' Combine the Current Row Range into the Delete Range.
                If drg Is Nothing Then
                    Set drg = rrg
                Else
                    Set drg = Union(drg, rrg)
                End If
            Else ' not a duplicate, so add it to the Dictionary
                dict(cString) = Empty
            End If
        'Else ' The Blank Column is not blank: do nothing.
        End If
    Next rrg
    
    If drg Is Nothing Then Exit Sub ' no duplicates found
    
    ' Delete the rows containing duplicates. Possible data to the right
    ' of the range will not be affected.
    drg.Delete ' for entire rows use drg.EntireRow.Delete
    
End Sub

【讨论】:

  • 感谢您提供这个详细的脚本。我按照说明进行操作,但似乎仍然找不到重复的行。我已经更新了上面的描述以进一步解释我的请求。谢谢
猜你喜欢
  • 2019-07-05
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 2022-01-27
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多