EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA String and Text Functions VBA Replace
Secondary Sidebar
VBA String and Text Functions
  • VBA String and Text
    • VBA Constants
    • VBA TRIM
    • VBA Find
    • VBA Case
    • VBA Replace
    • VBA String
    • VBA Split
    • VBA InStr
    • VBA RegEx
    • VBA Left
    • VBA UCASE
    • VBA LCase
    • VBA Concatenate
    • VBA StrComp
    • VBA StrConv
    • VBA Find and Replace
    • VBA Right
    • VBA ASC
    • VBA InStrRev
    • VBA Length of String
    • VBA Format
    • VBA MID
    • VBA Concatenate Strings
    • VBA SubString
    • VBA String Comparison
    • VBA String Array
    • VBA Dynamic Array
    • VBA Replace String

VBA Replace

By Ashwani JaiswalAshwani Jaiswal

VBA Replace Function

Excel VBA Replace Function

As in Excel, we have a function where we can find and replace any word or character or sentence with any letter. But by that process, we can only replace one kind of sentence or letter at a time. With the help of the VBA Replace Function, we can replace as many words or letters or sentences in a single shot. This saves huge time and effort doing single activity multiple times. For this, we will use the Replace function, which is in the built-in VBA function list.

Below the syntax and argument of Replace function in VBA.

Syntax of Replace

How to Use Excel VBA Replace Function?

We will learn how to use a VBA Replace Excel function with few examples.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,580 ratings)
You can download this VBA Replace Excel Template here – VBA Replace Excel Template

VBA Replace Function – Example #1

We have a sample data of some sentences where we will replace one word and paste that updated sentence in Sentence B Column.

Example 1-1

Follow the below steps to use the Replace function in Excel VBA.

Step 1: Go to VBA, and in the Insert menu tab, select Module.

VBA Replace Example 1-2

Step 2: Now, write a sub-category in the name of a performed function or in any name as per your choice, as shown below.

Code:

Sub VBA_Replace2()

End Sub

VBA Replace Example 1-3

Step 3: Now define a dimension as Long as we will be selecting a complete sentence. Here we have taken it as X.

Code:

Sub VBA_Replace2()

    Dim X As Long

End Sub

VBA Replace Example 1-4

Step 4: Now, in that Long X, select the maximum range till where our data could go up. Here we have taken as A1000 cell range, and for going up till our data starts, use End(xlUp) followed by a dot(.)Row. Which means we will be moving up in the same column till that row which has the data. This process in life Ctrl + Up arrow key in excel.

Code:

Sub VBA_Replace2()

    Dim X As Long
    X = Range("A1000").End(xlUp).Row

End Sub

VBA Replace Example 1-5

Note: We can select from End(xlDown), but it may not add any break or blank cell in between the data if data has.

Step 5: Now again, define one more dimension, Y, as Long as shown below.

Code:

Sub VBA_Replace2()

    Dim X As Long
    X = Range("A1000").End(xlUp).Row

    Dim Y As Long

End Sub

VBA Replace Example 1-6

Step 6: Now start a For Next loop for second defined dimension Y from cell position 2 to till X range (last filled cell)

Note: Selecting cell 2 means that we are not considering the header here for replace.

Code:

Sub VBA_Replace2()

    Dim X As Long
    X = Range("A1000").End(xlUp).Row

    Dim Y As Long

For Y = 2 To X

Next Y

End Sub

VBA Replace Example 1-7

Step 7: Now select the Value of Column B as Range as Y followed by a dot(.); this is like inserting a function in excel.

Code:

Sub VBA_Replace2()

    Dim X As Long
    X = Range("A1000").End(xlUp).Row

    Dim Y As Long

For Y = 2 To X

    Range("B" & Y).Value =
  
Next Y

End Sub

VBA Replace Example 1-8

Step 8: Now, as we have seen in the VBA syntax of Replace function above now type Replace and select Column A and Y as the first expression of string followed by the words which we need to replace from the table.

Code:

Sub VBA_Replace2()

    Dim X As Long
    X = Range("A1000").End(xlUp).Row

    Dim Y As Long

For Y = 2 To X

    Range("B" & Y).Value = Replace(Range("A" & Y), "Delhi", "New Delhi")

Next Y

End Sub

VBA Replace Example 1-9

Step 9: Now, compile the complete code and run. Once we do that, we will the word “Delhi” from Sentence A is now replaced with work “New Delhi”, and the whole sentence is copied to Sentence B.

Result of Example 1

We can select a string of words or a sentence and replace that with any required letters or sentence.

VBA Replace Function – Example #2

There is another way to replace words or sentences with the help of VBA coding. For this, we have a list of 7 subjects which are getting repeated. And we need to replace those subject names with adding serial numbers before each subject name, as shown below.

Example 2-1

As we can see in the above screenshot, column E has unique names of those subjects which are there in column B, and column F has the subject names with a serial number at the start of it.

Follow the below steps to use Replace function in VBA.

Step 1: Now, go to VBA and from the Insert menu, add a new Module. Once we get it, start writing the Sub-category in the name of a function which is being performed, as shown below.

Code:

Sub VBA_Replace()

End Sub

Sub-category Example 2-2

Step 2: Now consider a dimension Rng as Range, InputRng as Range and ReplaceRng as Range. You can choose any other letters or words for defining ranges.

Code:

Sub VBA_Replace()

Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range

End Sub

VBA Replace Example 2-3

Step 3: Now, use xTitleId as dialog box and give it a name. Here we have named it “VBA_Replace“.

Code:

Sub VBA_Replace()

Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range

    xTitleId = "VBA_Replace"

End Sub

VBA Replace Example 2-4

Step 4: Now assign the Application.Selection with InputRng, this will enable the selected application to be used in VBA.

Code:

Sub VBA_Replace()

Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range

    xTitleId = "VBA_Replace"
  Set InputRng = Application.Selection

End Sub

VBA Replace Example 2-5

Step 5: Now, in the next line, insert an InputBox of 3 types, Original range, xTitleId, and InputRng. Original Range is a list of those subject which are needed to get replaced, listed in column B. xTitledId is the dialog box names in the performed function. And InputRng is a range of data table in column E and F.

Code:

Sub VBA_Replace()

Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range

    xTitleId = "VBA_Replace"
    Set InputRng = Application.Selection 
    Set InputRng = Application.InputBox("Original Range ", xTitleId, InputRng.Address, Type:=8)

End Sub

VBA Replace Example 2-6

Step 6: Now, in the next line, assign an Input for ReplaceRng and for this, select Replace Range column. The rest of it is the same.

Code:

Sub VBA_Replace()

Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range

    xTitleId = "VBA_Replace"
    Set InputRng = Application.Selection
    Set InputRng = Application.InputBox("Original Range ", xTitleId, InputRng.Address, Type:=8)
    Set ReplaceRng = Application.InputBox("Replace Range :", xTitleId, Type:=8)

End Sub

VBA Replace Example 2-7

Step 7: Now, we will use the Application.screenupdating function, as the name says it is used for updating if it is FALSE.

Code:

Sub VBA_Replace()

Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range

    xTitleId = "VBA_Replace"
    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

End Sub

VBA Replace Example 2-8

Step 8: Now insert a For-Next loop. And for each Rng range, replace the values from ReplaceRng column.

Code:

Sub VBA_Replace()

Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range

    xTitleId = "VBA_Replace"
    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

    Next

End Sub

Example 2-9

Step 9: At last, replace InputRng with the values present in Rng from the whole sheet.

Code:

Sub VBA_Replace()

Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range

    xTitleId = "VBA_Replace"
    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:=xlWhole

  Next

End Sub

Example 2-10

Step 10: Once done, now compile and run the code. We will get and dialog box in the name of VBA_Replace. From here, select the list of subjects which we need to replace as Original Range and click on OK. Then we will get another box; from there, select the Replace range from E2 to F8. This table has the data which need to get replaced, and click on OK.

VBA Replace Example 2-11

We will get Data in column B, which will get replaced from the data in column F with a serial number.

Result of Example 2

To an overview, column E has unique subject names. And column F has subject names with serial numbers. So by this, data in column B is replaced with data in column F.

Pros of VBA Replace Function

  • We can replace multiple words or sentences in a single shot.
  • There is no limit of words or text what we cannot replace with.
  • Syntax of Replace in VBA is as easy as using the SumIf function in excel.
  • VBA Replace, as shown in example-1, is the easiest way to apply.

Things to Remember

  • Save as Marco Enabled Excel to avoid losing written code in VBA.
  • Always consider the dimensions in such a way that it will create a value-added while we select those in code.
  • Make sure you select the whole range of the sheet as shown in example-2 if it is limited to get the result. Or else you can define and select a limited cell range which would be used under Replace function.

Recommended Articles

This has been a guide to VBA Replace Function. Here we discussed VBA Replace and how to use Excel VBA Replace Function along with practical examples and a downloadable excel template. You can also go through our other suggested articles –

  1. VBA Loops
  2. VBA Do While Loop
  3. VBA Number Format
  4. VBA MsgBox
2 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more