TimeDiff Type Mismatch

csand

New Member
Joined
Jul 21, 2009
Messages
9
Code:
If ((DateDiff((Cells(nRow, 7).Value), Date, "d") > 1) & ((DateDiff(Date, (Cells(nRow, 8).Value), "d")) > 1)) Then
            Sheets("Sheet").Cells(nRow, 33) = "Active"
End If

When my macro runs through the above code, I get a type mismatch error. All of the cells are formatted at Date only.

Any ideas?

Thank you.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Your interval is backwards..

DateDiff((Cells(nRow, 7).Value), Date, "d")

Should be

DateDiff("d",(Cells(nRow, 7).Value), Date)
 
Upvote 0
Code:
If ((DateDiff((Cells(nRow, 7).Value), Date, "d") > 1) & ((DateDiff(Date, (Cells(nRow, 8).Value), "d")) > 1)) Then
            Sheets("Sheet").Cells(nRow, 33) = "Active"
End If

When my macro runs through the above code, I get a type mismatch error. All of the cells are formatted at Date only.

Any ideas?

Thank you.
you have the datediff order wrong

datediff("d",...
the interval goes first in 2007 at least
the worksheet fuctiopn datedif has the "d" at the end
 
Upvote 0
Code:
Sheets("CopyPaste").Select
If ((DateDiff("d", (Cells(nRow, 7).Value), Date) > 1) & ((DateDiff("d", Date,(Cells(nRow, 8).Value))) > 1)) Then
            Sheets("Sheet").Cells(nRow, 33) = "Active"
End If

I change the code and still have the same error.
 
Upvote 0
When it bugs out.. hit DEBUG

Then in the immediate window type
? typename(Cells(nRow, 7).Value)
and ensure that it's a date

do the same for Cells(nRow, 8).Value
 
Upvote 0
phx, thanks for the help.

I just tried your command in the debugger, and both cells give me the first cell's value (the second cell has a different value).
 
Upvote 0
if
Code:
? typename(Cells(nRow, 7).Value)
returns anything other then DATE then your formula won't work
 
Upvote 0
When I type that in it replaces the text with "7/1/2008" in red font.

Is that a valid value?
 
Upvote 0
Where are you typing this into ? Not the procedure window.. should be in the IMMEDIATE window.

If you don't see this window.. hit CONTROL+G
 
Upvote 0
Okay, i'm sorry for the confusion. I typed in the immediate window and both returned as Date on the next line when I pressed enter.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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