Help ! Replace using a macro

tglancy

New Member
Joined
Sep 12, 2002
Messages
10
In fields a1 and a2 I have two aplhanumeric values , call them "d0409" and
"d1309".

I simply need to use these values (although they change each week) as input to a replace but within a macro. For example

Find :- d2808
Replace with :- d0409 (Value of Cell a1).

I was given the following code but it doesnt seem to work :-

Selection.Replace What:="rrr", Replacement:="=a1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False


Remember , the a1 and A2 values are not constants.Can this be done ? Thanks , I'm sure this question will take some of you no time at all ! I'm a new user.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Two questions:

1. Does the "What" have a pattern?

2. What is the relationship between "What" and "Replacement"
 

tglancy

New Member
Joined
Sep 12, 2002
Messages
10
the two strings , before and after are aplhapnumeric date strings , eg today would be d1609. Unfortunately they do not follow a daily or weekly pattern , eg the last string could be d0909 or d2408.

Thanks
 

tglancy

New Member
Joined
Sep 12, 2002
Messages
10

ADVERTISEMENT

They are used as part of a cell link to another xls. eg :-

='G:Moseshypoth[output1309_nonb.xls]Column Output (2)'!O17/1000
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
I should have read your original posting more carefully.

This is how to send variables to the Replace method.

Code:
Sub Test()
   Dim What As String
   Dim With As String
   What = Range("A1").Text
   With = Range("A2").Text
   Selection.Replace What:=What, Replacement:=With, LookAt:=xlPart, _ 
SearchOrder:=xlByRows, MatchCase:=False
End Sub

I'm not sure what the Copy and PasteSpecial bit is for.

If you want to change links it is probably quicker to change the source. The macro recorder will give you the code and syntax.
 

Forum statistics

Threads
1,144,122
Messages
5,722,604
Members
422,447
Latest member
knopp

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