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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello, no it is just text: 'the'. Most, if not all, info that will be inputted into B for the ReplaceRng will be text.
 
Upvote 0
In that case the only way I can replicate that is with the ReplaceRng like


Excel 2013 32 bit
AB
21a
32b
43c
54d
65e
710
811g
921h
1036i
Upload
 
Upvote 0
Hello, I just re-ran it, and it worked perfectly! Thank you so much for all of your help!
 
Last edited:
Upvote 0
Glad it's sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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