Macro to Clear '

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,701
Office Version
  1. 2019
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
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
howard,

Here is macro solution for you to consider.

Sample raw data:

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>D</th><th>E</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">remove the '</td><td style=";">remove the '</td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style=";">the ' must be removed</td><td style=";">the ' must be removed</td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style=";">a ' b ' c</td><td style=";">a ' b ' c</td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Sheet1</p><br /><br />

And, after the macro:

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>D</th><th>E</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">remove the </td><td style=";">remove the </td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style=";">the  must be removed</td><td style=";">the  must be removed</td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style=";">a  b  c</td><td style=";">a  b  c</td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Sheet1</p><br /><br />

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.
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,701
Office Version
  1. 2019
Platform
  1. Windows
Thanks for the reply much appreciated
 

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
howard,

You are very welcome. Glad I could help.

Did the macro work for you?
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,701
Office Version
  1. 2019
Platform
  1. Windows
Hiker95

Thanks very much-the code worked 100%
 

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
howard,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,719
Messages
5,597,734
Members
414,170
Latest member
Mdm

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
Top