VBA - Runtime Error 13 on simple macro

EdwardSurrey

New Member
Joined
May 13, 2015
Messages
36
Office Version
  1. 365
Platform
  1. Windows
Hello. I am using the following code to search for any cells that contain " " within a set range, and then clear the contents of those cells. I get a Type Mismatch (Run-time Error 13) and the debug highlights that the
If cell.Value = " " Then part. It does seem to work on cells it look at until it errors. I've been struggling to solve all morning - am I missing something obvious? :)

Sub ClearDataV2()
Application.ScreenUpdating = False
For Each cell In Range("ImportData2")

If cell.Value = " " Then
cell.ClearContents
End If
Application.ScreenUpdating = True
Next
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Do any of the cells it is checking have errors in them?
 
Upvote 0
Rather than looping through the cells, you can do it in one hit like (this assumes that the cells do not contain a formula)
VBA Code:
Sub EdwardSurrey()
   Range("ImportData2").Replace " ", "", xlWhole, , , , False, False
End Sub
 
Upvote 0
Solution
I agree with Joe's question implying most likely error values in the range.

Edit: :oops: Too slow typing this part. :cool:

But why not do them all at once?
VBA Code:
Sub ClearDataV2a()
  Range("ImportData2").Replace What:=" ", Replacement:="", LookAt:=xlWhole
End Sub
 
Upvote 0
I agree with Joe's question implying most likely error values in the range.

Edit: :oops: Too slow typing this part. :cool:

But why not do them all at once?
VBA Code:
Sub ClearDataV2a()
  Range("ImportData2").Replace What:=" ", Replacement:="", LookAt:=xlWhole
End Sub

Aha, thanks! - But then how can I make it ignore the errors?
 
Upvote 0
Rather than looping through the cells, you can do it in one hit like (this assumes that the cells do not contain a formula)
VBA Code:
Sub EdwardSurrey()
   Range("ImportData2").Replace " ", "", xlWhole, , , , False, False
End Sub
Amazing - Works in an instant and without any errors at all :) Thank you!

Thanks also @Peter_SSs for your help.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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