Replace

The Replace method is used to replace a target string with the replacement string within range. It returns reference to a WksRange object.

Syntax

object. Replace( TargetStr, ReplaceStr, Current, Scope, Method, MatchCase, DelSelFirst, ReplaceAll, OrderByRows )

Parameter

Type

Required

/Optional

Default

Description

TargetStr

String

Required

This is the text to be found.

ReplaceStr

String

Required

This is the text with which the TargetStr should be replaced.

Current

WksRange

Required

This must contain reference to a single cell. The search will start at this cell. The search will include this cell, and all cells after it.

Scope

wksFindScope

Optional

wksFindScopeEntire

This is the equivalent to the search order. the Scope controls the direction of the search: down through columns, to the right across rows, or both.

Method

wksFindMethod

Optional

wksFindExact

The Search method determines how the search is performed. Find the exact information, a phrase, all words or any words.

MatchCase

Boolean

Optional

True

If the TargetStr parameter has case-sensitive words that need to be matched, then set MatchCase to be True.

DelSelFirst

Boolean

Optional

False

DelSelFirst should be set to True, to deselect all selected cells before performing the search.

ReplaceAll

Boolean

Optional

True

ReplaceAll should be set to True, to replace all occurrences of the Replace criteria in the work sheet.

OrderByRows

Boolean

Optional

True

OrderByRows should be set to True, if it should finish searching a row, before going to the next row and False, if it should finish searching a column, before going to the next column.

Example

The following example illustrates use of the Replace method. It replaces all occurrences of the number '4' in the worksheet with the text ' test'.

Dim SurferApp As Object

Set SurferApp = CreateObject("Surfer.Application")

SurferApp.Visible = True

'Open the worksheet document

Set Wks = SurferApp.Documents.Open("c:\temp\book1.txt")

'Specify the worksheet range to Columns A-B

Set WksRange = Wks.Columns(Col1:=1, Col2:=2)

'Replace all occurrences of the number '4' in the worksheet with the text ' test'

Set After = Wks.Range(1, "A", 1, "A") ' First cell

WksRange.Replace(TargetStr:="4", ReplaceStr:="test", Current:=After, _

Scope:=wksFindScopeEntire, Method:=wksFindPhrase, ReplaceAll:=True)

Used by: WksRange object