VBA Excel - 删除重复值并逐行重新排列非重复数据

VBA Excel - Delete the repeated values and rearrange non-repeated data line wise

提问人:HummBird 提问时间:10/5/2023 更新时间:10/5/2023 访问量:41

问:

我正在尝试组织从 A17 到 C30 开始的数据集

A18 (文件编号) B18 (文件标题) C18 (修订版)
星期一 R1型
星期一 R1型
星期一 R1型
星期一 黄色 R1型
星期二 R2型
星期三 R1型
星期三 R1型
星期四 绿 R3型

预期结果

答18 B18型 C18型
星期一 R1型
星期二 R2型
星期三 R1型
星期四 绿 R3型

我无法删除或插入任何行或列或过滤器来完成此任务。它会影响所有其他数据。
只能复制和粘贴

我尝试了下面的代码,但是我被困在重新排列空数据上。
因为我无法删除或过滤数据。我有凝灰岩时间来实现这一目标。请寻求您的帮助!

Sub ClearRowValues()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long

    ' Set the worksheet where your data is located
    Set ws = ThisWorkbook.Sheets("INPUT")
    
    ' Find the last row in column A with data
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ' Loop through rows from the bottom to the top
    For i = lastRow To 17 Step -1 ' Start from the second last row and move upwards
        If ws.Cells(i, 1).Value = ws.Cells(i - 1, 1).Value Then
            ' If the current Doc. No. is the same as the previous row, clear the current row
            ws.Cells(i, 1).ClearContents
            ws.Cells(i, 2).ClearContents
            ws.Cells(i, 3).ClearContents
        End If
    Next i
End Sub
Excel VBA6

评论

1赞 Foxfire And Burns And Burns 10/5/2023
您可以将数据通过VBA甚至Dictionary对象(这将有助于避免重复)带入数组中,执行您需要执行的操作并将输出粘贴到其他位置
0赞 HummBird 10/5/2023
你能帮我解决代码吗?

答:

1赞 JvdV 10/5/2023 #1

如果复制/粘贴是您唯一的选择,那么为什么不使用公式:

enter image description here

公式:E2

=UNIQUE(CHOOSEROWS(A2:C9,XMATCH(A2:A9,A2:A9)))

评论

0赞 HummBird 10/5/2023
是的,这是一个很好的选择,我现在会尝试一下,但问题是输出行是合并行。我知道使用合并列和行是不好的。但我需要这样做,因为对于某些打印用途。
0赞 Foxfire And Burns And Burns 10/5/2023
@HummBird问题是输出行是合并行 这是什么意思?如果您有 Excel365,此解决方案既简单又容易。
0赞 HummBird 10/5/2023
我只能访问 excel 标准 2021 版本:(
0赞 HummBird 10/5/2023
我期待上述结果的行是合并的单元格。在使用您提供的公式获取值时遇到麻烦。现在意识到我正在使用 excel365。