PeterB1988
New Member
- Joined
- Jun 26, 2015
- Messages
- 5
Hi,
I have been working on a VBA code for the last week or so and can get about 5% away from the result that I require.
I have 2 worksheets:
Sheet1 - which has a list of data in E:E
Sheet2 - which has a list of data in G2:G
Both lists have matching values in the list.
What I am trying to do is check if each cell in sheet1 is in sheet2, if it is then copy and paste the data from that row into the relative row of sheet2's matched value. This bit I can do........Where I am struggling is when I have a single value in sheet1 but multiple values in sheet2. I only want to copy and paste the data across in one match but it will copy and paste the info across to both matches. Similarly if I have 2 values in sheet1 but only one value in sheet2 it overwrites the information.
Below I have shown the code I am using.
Please help me. I am pulling my hair out!
Thanks
I have been working on a VBA code for the last week or so and can get about 5% away from the result that I require.
I have 2 worksheets:
Sheet1 - which has a list of data in E:E
Sheet2 - which has a list of data in G2:G
Both lists have matching values in the list.
What I am trying to do is check if each cell in sheet1 is in sheet2, if it is then copy and paste the data from that row into the relative row of sheet2's matched value. This bit I can do........Where I am struggling is when I have a single value in sheet1 but multiple values in sheet2. I only want to copy and paste the data across in one match but it will copy and paste the info across to both matches. Similarly if I have 2 values in sheet1 but only one value in sheet2 it overwrites the information.
Below I have shown the code I am using.
Sub FillinAlegs()
Application.ScreenUpdating = False
Dim names As Range, name As Range, values As Range, value As Range, rng As Range
Set values = Worksheets("Sheet1").Range("E1:E" & Worksheets("Sheet1").Range("E" & Rows.Count).End(xlUp).Row) '
Set names = Worksheets("Sheet2").Range("G2:G" & Worksheets("Sheet2").Range("G" & Rows.Count).End(xlUp).Row) '
For Each name In names
For Each value In values
If value.value = name.value Then
Worksheets("Sheet1").Range("A" & value.Row & ":P" & value.Row).Copy Destination:=Worksheets("Sheet2").Range("C" & name.Row & ":R" & name.Row)
End If
Next value
Next name
Application.ScreenUpdating = True
End Sub
Please help me. I am pulling my hair out!
Thanks