Macro to Clear '

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have set up a macro to clear ' where there appear in column D & E from data that was imported

When running the macro I get type mismatch and the following code is highlighted

Code:
 If Range("D2:E" & lr).Value = "'" Then


I would be appreciated if someone can kindly amend the code so as to clear the ' (apostrophe) wher it appears in Col D & E

Code:
 Sub Remove_Unwanted()
Dim lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row

    If Range("D2:E" & lr).Value = "'" Then
        Range("D2:E" & lr).ClearContents
    End If

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
howard,

Here is macro solution for you to consider.

Sample raw data:


Excel 2007
DE
1
2remove the 'remove the '
3the ' must be removedthe ' must be removed
4a ' b ' ca ' b ' c
5
Sheet1


And, after the macro:


Excel 2007
DE
1
2remove theremove the
3the must be removedthe must be removed
4a b ca b c
5
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub RemoveApostrophes()
' hiker95, 06/26/2015, ME864352
Dim Addr As String
Addr = "D2:E" & Cells(Rows.Count, "A").End(xlUp).Row
Range(Addr) = Evaluate(Replace("IF(LEN(@),SUBSTITUTE(TRIM(@),""'"",""""),"""")", "@", Addr))
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the RemoveApostrophes macro.
 
Upvote 0
Hiker95

Thanks very much-the code worked 100%
 
Upvote 0
howard,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,288
Members
448,563
Latest member
MushtaqAli

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