Find and Replace in VBA

– Selva V Pasupathy, HSBC Global Resourcing, Hyderabad

'____________________________________________________________
'   Author :     Selva V Pasupathy
'   Description:  The following macro code helps you find and replace
'                 by the use of vba
'____________________________________________________________
Sub Find_Replace(ByVal txtFind As String, ByVal txtReplace As String)
If Selection.Cells.Count = 1 Then
    ActiveSheet.Cells.Replace What:=txtFind, _
                    Replacement:=txtReplace, _
                    LookAt:=xlPart, _
                    SearchOrder:=xlByRows, _
                    MatchCase:=False, _
                    SearchFormat:=False, _
                    ReplaceFormat:=False
Else
    Selection.Replace What:=txtFind, _
                    Replacement:=txtReplace, _
                    LookAt:=xlPart, _
                    SearchOrder:=xlByRows, _
                    MatchCase:=False, _
                    SearchFormat:=False, _
                    ReplaceFormat:=False
End If
End Sub

Sub Test_Run()
  ' To find "2007" and replace with "2008" you can use following statement
  Call Find_Replace("2007", "2008")
End Sub

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: