VBA to remove Apostrophes works but also gives Run-time error 13. Unable to work out why?

Event2020

Board Regular
Joined
Jan 6, 2011
Messages
122
Office Version
  1. 2019
Platform
  1. Windows
Windows 10
Excel 2019


In Column K, the cells are populated by a formula.

In some cells the formula will have nothing to return ("") and Excel inserts a (') Apostrophe which I need to remove but only for the cells where the formula has returned nothing.


I have the code below that selects Cell K2 down to the last used row.

Then for any cell that is blank it removes the (') Apostrophe.

This works and all of the Apostrophes are remove but VBA does give a Run-time error '13' Type mismatch on the line of code
VBA Code:
If r.PrefixCharacter = "'" And Len(r.Value) = 0 Then

I would like to ask 2 questions if I may.

1. What is causing the error and how may it be fixed?
2. Is there a better (and faster) way to do this task?

This code will be inserted at the beginning of another sub, which is an advanced filter, to prepare Column K for the filter as the Apostrophes are causing the filter not to work correctly.
Once the Apostrophes are removed the filter works perfectly.

VBA Code:
Sub ClearApostraphie()
Dim sht As Worksheet
Dim LastRow As Long
Dim r As Range

Set sht = ActiveSheet

LastRow = sht.Range("K2").CurrentRegion.Rows.Count

sht.Range("K2", sht.Range("K" & LastRow)).Select

For Each r In ActiveSheet.UsedRange
        If r.PrefixCharacter = "'" And Len(r.Value) = 0 Then
            r.Clear
        End If
    Next r
End Sub
 
Based on that and your previous responses change your code to this:
VBA Code:
Sub ClearApostraphie()
    Dim sht As Worksheet
    Dim LastRow As Long
   
    Set sht = ActiveSheet
   
    LastRow = sht.Range("K" & Rows.Count).End(xlUp).Row
   
    With sht.Range("K2:K" & LastRow)
        .Value = .Value
    End With

End Sub

As an aside and in case it helps anyone else reading this thread I have been able to do away with the seperate SUB that was changing the formulas in Column K to thier calculated values by add a single line of code in to yours as yours was already selecting the range that had data, or where the formula returned data.

I added
VBA Code:
.Formula = .Value
inside the "With" like so
VBA Code:
With sht.Range("K2:K" & LastRow)
    .Formula = .Value
        .Value = .Value


    End With

Thank you again Alex.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I'm not clear on what your added line of code does. I would not expect any difference between this
VBA Code:
    With sht.Range("K2:K" & LastRow)
        .Formula = .Value
        .Value = .Value
    End With
and just this
VBA Code:
    With sht.Range("K2:K" & LastRow)
        .Value = .Value
    End With
 
Upvote 0
I'm not clear on what your added line of code does. I would not expect any difference between this
VBA Code:
    With sht.Range("K2:K" & LastRow)
        .Formula = .Value
        .Value = .Value
    End With
and just this
VBA Code:
    With sht.Range("K2:K" & LastRow)
        .Value = .Value
    End With
Your right - a rooky error on my part I'm afraid.
 
Upvote 0

Forum statistics

Threads
1,224,928
Messages
6,181,811
Members
453,067
Latest member
mdiz777

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