Search range and replace data

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,602
Office Version
  1. 365
Platform
  1. Windows
Hi all,

In sheet1 cell A2 I have a ref number eg 1234567
I would like a macro to search through cells A12:A100 of sheet2 for the same ref number and when found replace the data in that row (columns A:O) with the data held in columns A:O of sheet1
any ideas please?
many thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
*Untested so I may make typos*

Code:
Dim Cell as variant

For Each Cell in Sheets("Sheet2").Range("A12:A100")
    If Cell.Value = Sheets("Sheet1").Range("A2").Value Then
           Cell.EntireRow.Value = Sheets("Sheet1").Range("A2").EntireRow.Value
    End if
Next

I'm sure there is a neater way without the loop..
 
Upvote 0
Hi,

To my sense you can
1. autofilter column A for your reference number
2. copy
3. paste in filtered sheet: entire range at once (without loop)
even manually, it would take less than 1 minute

If that works for you, you can record your operations (using macro recorder) and you will have useful code. If you want help to edit the code, then display it here.

kind regards,
Erik
 
Upvote 0
Hi Makrini and thanks for the code..

Code:
Dim Cell as variant

For Each Cell in Sheets("Sheet2").Range("A12:A100")
    If Cell.Value = Sheets("Sheet1").Range("A2").Value Then
           Cell.EntireRow.Value = Sheets("Sheet1").Range("A2").EntireRow.Value
    End if
Next

..it almost works, but just places a value of '1' in the correct row in sheet2 instead of the whole row of data from sheet1 A:O

and ideas?
 
Upvote 0
Hi erik,

Yep, thanks just got it all to work as I wanted, so I guess its a big thanks to you and makrini for all you efforts.,

Once again thanks !
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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