2017-02-16 312 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