Macro to find and replace data from one sheet to another

PostIt

New Member
Joined
Jun 3, 2015
Messages
11
Hello,

I need to find and replace data (A2:B632) from Sheet 2 to Sheet 1, but I only want to replace the data for Column F to Column AM.
So that if the name in Column A of Sheet 2 matches any name in Sheet 1 (Column F - Column AM), the cell will be replaced with the cost which is given in Column B.
Is there a macro for that?

Or will I have to input separate Find and Replace macros?

Please advise.
Thank you!

Running:
Windows 7 Home Premium
Microsoft Office Home and Student 2010
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

LloydFinancials

Well-known Member
Joined
Apr 24, 2015
Messages
541
Hello PostIt,

You can give this a try:

Code:
Sub findReplace()
  Dim i As Long
  Dim c As Range
  Dim r As Range
  On Error Resume Next
  For i = 2 To Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    Set c = Worksheets("Sheet2").Range("A" & i)
    Worksheets("Sheet1").Select
    Worksheets("Sheet1").Range("F:AM").Select
    Selection.Replace What:=c.Value, Replacement:=c.Offset(, 1), LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False
  Next i
  Worksheets("Sheet2").Select
  Worksheets("Sheet2").Range("A1").Select
End Sub

Luke
 
Upvote 0

PostIt

New Member
Joined
Jun 3, 2015
Messages
11

Brilliant.
I don't know what it is, but this macro seems to be taking a really long time.
So far, the replacements seem to be going smoothly though.
Thank you!
 
Upvote 0

LloydFinancials

Well-known Member
Joined
Apr 24, 2015
Messages
541
Thank you for the feedback. Is there any way to update the range F:AM to something like F1:AM9999. Decreasing this range should help.
 
Upvote 0

PostIt

New Member
Joined
Jun 3, 2015
Messages
11
I'm pretty sure there is but I'm not exactly super excel savvy, but I will look into it.
Thank you. :)
 
Upvote 0

Forum statistics

Threads
1,195,902
Messages
6,012,202
Members
441,678
Latest member
RewtX

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
Top