Help!! Not sure how to write this simple code. NEWBIE Find and Replace

LukeWayne

New Member
Joined
Feb 18, 2017
Messages
24
I am working on a <acronym title="visual basic for applications">VBA</acronym> to Find from Column B (Sheet 2) and Find in (Sheet 1) and Replace the value = Column B in Sheet 2 of which = Column A (the replacement value.) When I run the macro it works, but I have multiple values = to the same value in Column B and it is only choosing one of those values to replace. How do I write this code to let it replace with multiple values it is equal to.

Here is the code:

Option Explicit
Option Compare Text

Sub FIND_AND_REPLACE()
On Error Resume Next
Application.ScreenUpdating = False

Dim toFind As String, toReplace As String, rng As Range, cel As Range, i As Long, frow As Long, _
frowT As Long, wk As Worksheet, ws As Worksheet, j As Long
Set wk = Sheet1: Set ws = Sheet2

frow = wk.Range("AM" & Rows.Count).End(xlUp).Row
frowT = ws.Range("A" & Rows.Count).End(xlUp).Row

Set rng = wk.Range("AM2:AQ" & frow)

For i = 2 To frowT
toFind = ws.Range("B" & i).Value
toReplace = ws.Range("A" & i).Value
rng.Replace What:=toFind, Replacement:=toReplace, LookAt:=xlWhole, MatchCase:=False
Next i

For i = 2 To frow
wk.Range("AR" & i) = ""

For j = 39 To 43
If Trim(wk.Cells(i, j)) <> "" Then
wk.Range("AR" & i) = wk.Range("AR" & i) & "," & Trim(wk.Cells(i, j))
End If
Next j

If Trim(wk.Range("AR" & i)) <> "" Then
wk.Range("AR" & i) = Right(wk.Range("AR" & i), Len(wk.Range("AR" & i)) - 1)
End If

Next i

Application.ScreenUpdating = True
MsgBox "Done"
End Sub
 
There is only one. Sheet 1 has one "3130LF" and is = 64.946 & 10.494 from Sheet 2. And by the way I am only working with one column in Sheet 1...
Sorry, I see no logic and don't understand why you posted the code tailored to a different project.
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Sorry, I see no logic and don't understand why you posted the code tailored to a different project.

I used a previous VBA code because it has similar use. I am not experienced at this at all! I'm just trying to get it to replace a column in Sheet 1 with what it's equal to in Sheet 2. (Sheet 2 Column A=Column B)=Sheet 1 Column replacement) In some cases it may be equal to two results because the manufacture numbers repeats in Column B in Sheet 2 as seen in the picture above. Right now this macro works in a way that only replaces Sheet 1 Column with only one value where it could have been two values. I want both values from Column A of Sheet 2.
 
Upvote 0

Forum statistics

Threads
1,216,586
Messages
6,131,571
Members
449,655
Latest member
Anil K Sonawane

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