VBA to Copy Sheet1.Cell to Sheet2.Cell if sheet1.cell = sheet2.cell

Mgas

New Member
Joined
May 14, 2020
Messages
2
Office Version
  1. 2007
Platform
  1. Windows
I am an excel VBA novice and get lots of errors when writing VBA to manipulate Excel sheet data.

I get lost with the Excel object concepts/code

The following psuedo-code shows what I want to do

Sheet1.Range(a1b1:c1d1) ' where a1,c1 = row and b1,d1 = col

Sheet2.Range(a2b2:c2d2) ' where a2,c2 = row and b2,d2 = col

For rw1 = a1 to c1 ' outer loop
For rw2 = a2 to c2 ' inner loop
if Sheet1.Cell(rw1:A) = Sheet2.Cell(rw2:A)
Copy Sheet2.Cell(rw2:D) to Sheet1.Cell(rw1:G)
Escape/Exit loop ' escape/exit inner loop
EndiF
EndFor 'end inner loop
EndFor 'end outer loop

Basically ..for each row in sheet 1, find a match in sheet2 and then copy a cell from the matching row in Sheet2 to the corresponding sheet1 row.

Can anyone help me with the VBA to do the above? its fairly urgent and I cant spare my usual trial and error time to try and figure it out.

Many thanks
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Mgas()
    Application.ScreenUpdating = False
    Dim Cl As Range
    
    With CreateObject("scripting.dictionary")
      For Each Cl In Sheet2.Range("A2", Sheet2.Range("A" & Rows.Count).End(xlUp))
         .Item(Cl.Value) = Cl.Offset(, 3).Value
      Next Cl
      For Each Cl In Sheet1.Range("A2", Sheet1.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then Cl.Offset(, 6).Value = .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
Hi
This worked fine, first time. Thanks very much,:):).

I would never have thought of coding it like this. it took me a while to work out how it worked.

Regards and thanks again
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,764
Messages
6,126,750
Members
449,335
Latest member
Tanne

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