View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Frank RoadRunner[_2_] Frank RoadRunner[_2_] is offline
external usenet poster
 
Posts: 1
Default epression REPLACE

I need to replace a string in a range of cells. I use a syntax below. The
purpose is to replace a string in a formula by the new one and add a new item.

Formula in Excel Workbook:
=================
=Average(January!prumDS12;February!prumDS12;March! prumDS12;April!prumDS12;prumDS12) where expression "prumDS12" is a named range.

Sub ReplRefer()

DIM ORIG as Variant
DIM NEW as Variant

Rem This part of the code works properly
'================================================
'copying formula in a new sheet

Sheets(PrevSheet).[YearAver].Copy
Sheets(ActSheet).[YearAver].Select
Selection.PasteSpecial Paste:=xlFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

'setting values to variables

ORIG = ";PrumDS12"
NEW = ";" & PredList & "!PrumDS12"
'=================================================
Rem In this part the Macro goes through the instructions without announcing an
error but the formula remains unchanged in the end.

'Replacing the string in the variable ORIG by the string in the variable NEW
'[YearAver] is a range in a workbook

[YearAver].Replace What:=ORIG, Replacement:=NEW, LookAt:= _
xlPart, SearchOrder:=xlByColumns, MatchCase:=False

ORIG = ")"
NEW = ";PrumDS12)"

[YearAver].Replace What:=ORIG, Replacement:=NEW, LookAt:= _
xlPart, SearchOrder:=xlByColumns, MatchCase:=False

End Sub

Where is the mistake? Is there anybody who would be able to help me? Many
thanks for eventual answer. I have a similar case where the name of a month
beeing replaced. It works. In spite of the fact I did the code above
analogically it doesn't work properly.

Frank