2017-02-16 311 views
-2

我有一个两列的excel文件。下面 Excel的截图:Excel VBA:复制并粘贴其他单元格的重复值

enter image description here

我要的是一个Excel VBA将读取所有重复的值,如果它旁边的单元格为空,从其他重复户口价值将在空白单元格粘贴。 预期结果:

enter image description here

我不擅长与Excel VBA,所以我会很感激你的帮助。

谢谢!

回答

1

的一个出发点是遍历每个值和列比较它的每个值:

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 
+0

您好,VBA皮特选择空单元及其对应的细胞。有效。非常感谢你! – user3445540

+0

不用担心@ user3445540 –

1

你可以试试这个

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

  • 的1st SpecialCells选择带有文本值的A列单元格

  • 偏移选择在下一列到右边(即B列)

  • 的第二SpecialCells在该后一范围

+0

嗨,@ user3598756。你的代码也工作。非常感谢你! – user3445540

+0

不客气。你可能想用你实际选择的代码来接受答案。 – user3598756

+0

@ user3445540,请通过接受它实际解决在代码中使用的答案来回报 – user3598756