Search and Replace - Leading Apostrophe

fbohlandt

New Member
Joined
Sep 17, 2021
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi all

I would like to search and replace for leading apostrophes. I have two pieces of code. The first recognizes the leading apostrophe but is slow in execution (loop through cells in range). The second is fast but ignores the apostrophe.

Code 1:

VBA Code:
Sub RemoveApostrophe()   
For Each cell In Selection       
    cell.Value = Replace(cell.Value, "'", "")   
Next cell
End Sub

Code 2:

Code:
Sub FindReplaceAll()

Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant

fnd = "'"
rplc = ""

'Store a specfic sheet to a variable
  Set sht = Sheets("Sheet1")

'Perform the Find/Replace All
  sht.Cells.Replace what:=fnd, Replacement:=rplc, _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
    SearchFormat:=False, ReplaceFormat:=False

End Sub

Essentially, I am looking for a macro that runs over a fairly large range in a worksheet (~ 10,000 cells) and replace any leading apostrophes/high commas whilst minimizing runtime. I am not sure I can avoid addressing each cell individually but hoping there is another way.

Thank in advance
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
If your worksheet only contains data and no formula then it will be much faster to load the entire worksheet into a variant array , process the worksheet in memory and then write it back.
if this is true : try this code:
VBA Code:
Sub test()
inarr = Worksheets("sheet1").UsedRange
 For i = 1 To UBound(inarr, 1)
  For j = 1 To UBound(inarr, 2)
     inarr(i, j) = Replace(inarr(i, j), "'", "")
  Next j
 Next i
Worksheets("sheet1").UsedRange = inarr
 
End Sub
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,215,364
Messages
6,124,507
Members
449,166
Latest member
hokjock

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