I'm new to VBA, so please forgive me in advance.
What I have is two worksheets, one with all of my raw data (Base) and the other housing information submitted from a form (Sheet 1).
What I am trying to do is finding any duplicate ID numbers from column B in Sheet 1 that match the IDs in Column D in the Base data, and if so, then copy all of the row from the Base sheet into Column J of the matching ID row on Sheet 1.
I've tried the below code, which works to an extent with the following issues that I'm unsure of how to fix:
What I have is two worksheets, one with all of my raw data (Base) and the other housing information submitted from a form (Sheet 1).
What I am trying to do is finding any duplicate ID numbers from column B in Sheet 1 that match the IDs in Column D in the Base data, and if so, then copy all of the row from the Base sheet into Column J of the matching ID row on Sheet 1.
I've tried the below code, which works to an extent with the following issues that I'm unsure of how to fix:
- Currently this code is looking for a fixed ID number (111467749), and I need this to instead look for duplicated IDs from Column B in sheet 1 that match Column D in Base.
- When deployed, it pastes the base data into a new row, instead of pasting it into the matching row, starting in column J.
VBA Code:
Dim xRg As Range
Dim xCell As Range
Dim A As Long
Dim B As Long
Dim C As Long
A = Worksheets("Base").UsedRange.Rows.Count
B = Worksheets("Sheet1").UsedRange.Rows.Count
If B = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Sheet1").UsedRange) = 0 Then B = 0
End If
Set xRg = Worksheets("Base").Range("D1:D" & A)
On Error Resume Next
Application.ScreenUpdating = False
For C = 1 To xRg.Count
If CStr(xRg(C).Value) = "111467749" Then
xRg(C).EntireRow.Copy Destination:=Worksheets("Sheet1").Range("A" & B + 1)
B = B + 1
End If
Next
Application.ScreenUpdating = True
End Sub