DateDiff gives Type mismatch error 13?

karinfromsweden

New Member
Joined
Oct 30, 2018
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hey all,
I have this neat little macro that runs on every start of a sheet. It's (supposed to be) pretty simple, checking for rows that are older than 180 days and then deleting part of the content of the row. My boss was so happy over this GDPR-compliant macro. :rolleyes:
But then a few days ago it started giving "run time error 13 Type mismatch".

I have doublechecked that the column only holds correctly formatted dates. And from what I can see, the macro works. So why does it give an error?
Any help would be dearly appriciated! Thank you!

Code:
Sub ClearCells()
Dim Cel As Range, Ws As Worksheet
Set Ws = Sheets("START")
For Each Cel In Ws.Range("A5", Ws.Range("A" & Rows.Count).End(xlUp))
    If DateDiff("d", Cel, Date) > 180 And Cel.Offset(, 0) <> "" Then Cel.Offset(, 6).Resize(, 3).ClearContents
Next Cel
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi Karin, without any tests: Have you checked, that there are only dates in column "A"? regards
 
Upvote 0
When you get the error what is the value in Cel?
 
Upvote 0
try running this to tell you which cells have got problems
Code:
Sub ClearCells()
    Dim Cel As Range, Ws As Worksheet, Msg As String
    Set Ws = Sheets("START")
    For Each Cel In Ws.Range("A5", Ws.Range("A" & Rows.Count).End(xlUp))
        On Error Resume Next
        If DateDiff("d", Cel, Date) > 180 And Cel.Offset(, 0) <> "" Then Cel.Offset(, 6).Resize(, 3).ClearContents
        If Err.Number > 0 Then Msg = Msg & vbCr & Cel.Address & vbTab & Cel.Value
        On Error GoTo 0
    Next Cel
    MsgBox Msg, vbCritical, "Problem cells"
End Sub
 
Last edited:
Upvote 0
Why use Cel.Offset(, 0) ?
Without any offset in any direction it is the same as Cel
 
Last edited:
Upvote 0
try running this to tell you which cells have got problems
Code:
Sub ClearCells()
    Dim Cel As Range, Ws As Worksheet, Msg As String
    Set Ws = Sheets("START")
    For Each Cel In Ws.Range("A5", Ws.Range("A" & Rows.Count).End(xlUp))
        On Error Resume Next
        If DateDiff("d", Cel, Date) > 180 And Cel.Offset(, 0) <> "" Then Cel.Offset(, 6).Resize(, 3).ClearContents
        If Err.Number > 0 Then Msg = Msg & vbCr & Cel.Address & vbTab & Cel.Value
        On Error GoTo 0
    Next Cel
    MsgBox Msg, vbCritical, "Problem cells"
End Sub


Wow! This gave me an empty cell, but I did Clear > All and now it works. Weird. THANK YOU!
 
Upvote 0
Not all empty-looking cells are truly empty! ;)
Possibly contained an errant space
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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