【问题标题】:Excel VBA: Copy and paste duplicate values from other cellExcel VBA:复制并粘贴其他单元格中的重复值
【发布时间】:2017-07-04 23:26:58
【问题描述】:

我有一个包含两列的 excel 文件。 Excel截图如下:

我想要的是一个 Excel VBA,它将读取所有重复值,如果它旁边的单元格是空白的,来自另一个重复帐户的值将被粘贴到空白单元格中。 预期结果:

我不擅长 Excel VBA,因此非常感谢您的帮助。

谢谢!

【问题讨论】:

    标签: vba excel duplicates


    【解决方案1】:

    你可以试试这个

    Sub Main()
        With Columns(1).SpecialCells(xlCellTypeConstants, XlTextValues).Offset(,1).SpecialCells(xlCellTypeBlanks)
            .FormulaR1C1 = "=R[-1]C"
            .Value = .Value
        End With
    End Sub
    

    在哪里

    • 第一个 SpecialCells 选择具有某些文本值的 A 列单元格

    • Offset 选择它们在右侧下一列(即 B 列)中对应的单元格

    • 第二个 SpecialCells 选择后一个范围内的空单元格

    【讨论】:

    • 嗨,@user3598756。您的代码也有效。非常感谢!
    • 不客气。您可能希望使用您实际选择的代码来接受答案。
    • @user3445540,请接受您实际决定在代码中使用的答案来奖励它
    【解决方案2】:

    一个起点是循环遍历每个值并将其与列中的每个值进行比较:

    Sub FillDuplicates()
    Dim lastrow As Long
    
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row 'find last row in column A
    
    For x = 1 To lastrow
        If Cells(x, 2).Value <> "" Then 'Check if cell in column B is empty
            For y = 1 To lastrow
                If Cells(y, 1).Value = Cells(x, 1).Value Then 'Compares cell against each value in column A
                    Cells(y, 2).Value = Cells(x, 2).Value 'If matches, add value in column B
                End If
            Next y
        End If
    Next x
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      相关资源
      最近更新 更多