VBA to find and replace from separate workbook / sheet

Bowler

New Member
Joined
Mar 12, 2018
Messages
7
Hello!
I am currently trying to find a Macro that can be used to find and replace multiple values in one cell (the cell content and location can change). I am a complete novice, and am really not sure what I am doing!

I have Sheet1 that contains the following information:

A B
1 This is
2 The text
3 I would like to be copied
4 Into my workbook
-
50 in full

In another sheet in the workbook (which is re-named frequently) I have cells with multiple numerical values which correspond to the text in cell B. E.g. in G4 I will have: 1, 2, 3, 50

I would like to run a macro which looks up the numbers in selected cell(s), and replaces it with the text. So:
1, 2, 3, 50 should become: This is, The text, I would like to be copied, in full

I currently have VBA code that successfully finds and replaces the values, but only up to 9. Then it starts replacing only part of double digits (10, 11 etc.) I know that this is due to:
LookAt:=xlPart in the VBA but when changed to
xlwhole, it does not work at all.


Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range
xTitleId = "iThoughts"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Original Range ", xTitleId, InputRng.Address, Type:=8)
Set ReplaceRng = Application.InputBox("Replace Range :", xTitleId, Type:=8)
Application.ScreenUpdating = False
For Each Rng In ReplaceRng.Columns(1).Cells
InputRng.Replace What:=Rng.Value, Replacement:=Rng.Offset(0, 1).Value, LookAt:=xlPart
Next
Application.ScreenUpdating = True
End Sub

I am not sure how to change the code to allow it to find and replace numbers past 9... if there is anyway anyone could help, that would be great!

Thank you in advance!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Cross posted https://www.excelforum.com/excel-pr...and-replace-from-separate-workbook-sheet.html

Cross-Posting
While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
The easiest way would be to sort col A in descending order, so that you replace 11 before 1
 
Upvote 0
The other option is
Code:
Dim Rng As Range, i As Long
Dim InputRng As Range, ReplaceRng As Range
xtitleId = "iThoughts"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Original Range ", xtitleId, InputRng.Address, Type:=8)
Set ReplaceRng = Application.InputBox("Replace Range :", xtitleId, Type:=8)
Application.ScreenUpdating = False
For i = ReplaceRng.Count To 1 Step -1
InputRng.Replace What:=ReplaceRng(i), Replacement:=ReplaceRng(i).Offset(0, 1).Value, LookAt:=xlPart
Next
Application.ScreenUpdating = True
 
Upvote 0
Hi, This is great, thank you. And thank you for posting the link (apologies).

It works for values above 9 now! but it does not work for the number 10?
 
Upvote 0
In what way does it not work for 10?
 
Upvote 0
Hiya, It just deletes the number but does not replace it.
So if I type in 1, 10, 11 in the cell, 1 and 11 will be replaced by the correct text, but 10 will be removed, resulting something that looks like: This is, , End
 
Upvote 0
Do you have anything in the cell next to 10?
ie if 10 was in A5, do you have anything in B5
 
Upvote 0
Hello, nothing is in the adjacent cell at the moment. But the macro will be used when there is data in other cells in the sheet.
So it needs to work when the workbook looks like: A5 contains: 1, 10, 11 B5 contains: 2, 3, 4, 10.
 
Upvote 0
I was talking about the ReplaceRng rather than the InputRng
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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