Matching Then Copy/Pasting in VBA

Kramer328

New Member
Joined
Oct 17, 2018
Messages
7
Hey everyone,

I am very new to VBA Programming so let me know if anyone needs more information. I am trying to write a VBA macro that will search columns F, H, and J in Sheet1 of my workbook and compare those values to column A of Sheet2. If there is a match, I would like to copy the values on Sheet2 that are 3 cells over (to the right) from the match on Sheet2 and paste that value one cell over (to the right) to the matching value in Sheet1. For example, if the first table is 'Sheet1' and the second table is 'Sheet2', I would like the result to look like the third table. Any help would be greatly appreciated. Thanks!

[TABLE="width: 500"]
<tbody>[TR]
[TD]SHEET1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[TD]K[/TD]
[/TR]
[TR]
[TD]Find1[/TD]
[TD][/TD]
[TD]Find2[/TD]
[TD][/TD]
[TD]Find3[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

[TABLE="width: 500"]
<tbody>[TR]
[TD]SHEET2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]Find1[/TD]
[TD][/TD]
[TD][/TD]
[TD]1.5
[/TD]
[/TR]
[TR]
[TD]Find2[/TD]
[TD][/TD]
[TD][/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Find3[/TD]
[TD][/TD]
[TD][/TD]
[TD]2.5[/TD]
[/TR]
</tbody>[/TABLE]

[TABLE="width: 500"]
<tbody>[TR]
[TD]SHEET1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[TD]K[/TD]
[/TR]
[TR]
[TD]Find1[/TD]
[TD]1.5[/TD]
[TD]Find2[/TD]
[TD]2[/TD]
[TD]Find3[/TD]
[TD]2.5[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi & welcome to MrExcel.
How about
Code:
Sub GetValues()
   Dim Cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("Sheet2")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         .Item(Cl.Value) = Cl.Offset(, 3).Value
      Next Cl
      For Each Cl In Intersect(Ws1.UsedRange, Ws1.Range("F:F,H:H,J:J"))
         Cl.Offset(, 1).Value = .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
Hey Fluff Thanks so much this is working great. The only problem I am having is that when the macro finishes, the values in cells G1, I1, K1 are cleared and I can't figure out why.
 
Upvote 0
I should be more specific. The first row in my worksheet (Sheet1) contains headers so they don't match any values in Sheet2. However, the values in G1, I1 and K1 are cleared after running the macro.
 
Upvote 0
Ok, as long as you don't have any blank cells in sheet2, try
Code:
Sub GetValues()
   Dim Cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("Pcode")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         .Item(Cl.Value) = Cl.Offset(, 3).Value
      Next Cl
      For Each Cl In Intersect(Ws1.UsedRange[COLOR=#ff0000].Offset(1)[/COLOR], Ws1.Range("F:F,H:H,J:J"))
         Cl.Offset(, 1).Value = .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,739
Messages
6,174,217
Members
452,551
Latest member
croud

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top