Excel VBA - Find & Replace (Faster Method?)

TropicalMagic

New Member
Joined
Jun 19, 2021
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hi all,



I would like to find and replace all values, from “Viet Nam” to “Vietnam” in range A.



However, I found that finding and replacing values cell by cell is extremely slow, especially if the there are 1000+ rows in the range A

I would like to use Excel VBA and arrays as arrays speeds up the automation process greatly. I am also open to using scripting dictionaries.


Here is my worksheet layout:

SCREENSHOT 1.png




Here is my code so far (does not work):



```

Dim ws As Worksheet

Dim x, LastRow As Long

Dim arrA As Variant



Set ws = Workbooks("MyWorkbook.xlsx").Sheets("MyWorksheet")

LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row



arrA = ws.Range("A2:A" & LastRow).Value 'put the range in an array to make the iteration faster



For x = 1 To UBound(arrA)



If arrA = “Viet Nam” Then

arrA(x, 1) = “Vietnam”

End If

Next x

```



How do I fix the issue?



Many thanks!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Select the column

- ctrl+H
- find what: Viet Nam
-Replace with: Vietnam
 
Upvote 0
@TropicalMagic Here is your wish for vba approach with array:

VBA Code:
Sub FindVietnam()
'
    Dim x       As Long
    Dim LastRow As Long
    Dim arrA()  As Variant
'
    LastRowColumnA = Sheets("MyWorksheet").Range("A" & Rows.Count).End(xlUp).Row
'
    arrA = Application.Transpose(Sheets("MyWorksheet").Range("A2:A" & LastRowColumnA))                  ' Load all country names into a 1D array
'
    For x = 1 To UBound(arrA)
        If StrComp(Replace(arrA(x), " ", ""), "Vietnam", vbTextCompare) = 0 Then arrA(x) = "Vietnam"    ' Compare to "Vietnam" with spaces removed & upper or lower case
    Next x
'
    Sheets("MyWorksheet").Range("A2:A" & LastRowColumnA) = Application.Transpose(arrA)                  ' Write array back to Column A
End Sub
 
Upvote 0
Solution
This will do in VBA

VBA Code:
Sub jec()
Range("A1", Cells(Rows.Count, 1).End(xlUp)).Replace "Viet Nam", "Vietnam"
End Sub
 
Upvote 0
@TropicalMagic Here is your wish for vba approach with array:

VBA Code:
Sub FindVietnam()
'
    Dim x       As Long
    Dim LastRow As Long
    Dim arrA()  As Variant
'
    LastRowColumnA = Sheets("MyWorksheet").Range("A" & Rows.Count).End(xlUp).Row
'
    arrA = Application.Transpose(Sheets("MyWorksheet").Range("A2:A" & LastRowColumnA))                  ' Load all country names into a 1D array
'
    For x = 1 To UBound(arrA)
        If StrComp(Replace(arrA(x), " ", ""), "Vietnam", vbTextCompare) = 0 Then arrA(x) = "Vietnam"    ' Compare to "Vietnam" with spaces removed & upper or lower case
    Next x
'
    Sheets("MyWorksheet").Range("A2:A" & LastRowColumnA) = Application.Transpose(arrA)                  ' Write array back to Column A
End Sub

This looks good! It worked as I intended! Many thanks!!

Also, thanks for deriving an array-based solution, it reduced the duration required!
 
Upvote 0
Ummm, why use VBA when a simple Find/Replace of column "A" would do ??....as suggested by @*JEC
 
Upvote 0
Ummm, why use VBA when a simple Find/Replace of column "A" would do ??....as suggested by @*JEC
Hi, thanks for your query!

Well, this issue is a small part of a large and frequent (weekly) report generation. I'm not the owner of the main file which I obtain the data from, and the main file will always show entries as "Viet Nam".

Hence, I would prefer to use VBA to automate the manual, repetitive component of editing to "Vietnam", as well as reduce the duration required to run the automation by using arrays since the range can contain as many as 1000+ rows.
 
Upvote 0
That's OK...now that you have provided further information for the need to do it by VBA, it makes sense to do it that way... :cool: (y)
 
Upvote 0

Forum statistics

Threads
1,214,549
Messages
6,120,149
Members
448,948
Latest member
spamiki

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
Back
Top