VBA - Type Mismatch error in date subtraction macro

dtrynoski

New Member
Joined
Aug 23, 2017
Messages
9
I don't know why I'm getting a Type Mismatch error. I'm trying to do a simple subtraction macro with a variable row spreadsheet and getting a Type Mismatch error on this line of code: I'm subtracting two dates, column I which contains expiration date must cell "AA1" which contains today's date.

Range("M" & i).Value = Range("I" & i).Value - Range("AA1").Value


I'm subtracting two dates, column I which contains expiration date must cell "AA1" which contains today's date.

My macro is as follows:

'exception age calculation
Range("M1").Select
ActiveCell.FormulaR1C1 = "Days to Expiration"
Range("AA1").Value = Date

With Worksheets("Archer Search Report")
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To FinalRow
Range("M" & i).Value = Range("I" & i).Value - Range("AA1").Value
Next i
End With
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
When using a With Structure, you need to proceed objects you want associated with that With, with a period..
I'm guessing the Range("AA1") that you put Date in is NOT in that With reference, but the other 2 ranges are.

Try
Rich (BB code):
With Worksheets("Archer Search Report")
     FinalRow = .Cells(Rows.Count, 1).End(xlUp).Row
     For i = 2 To FinalRow
         .Range("M" & i).Value = .Range("I" & i).Value - Range("AA1").Value
     Next i
End With
 
Last edited:
Upvote 0
Ok, which sheet are each of the 3 cells on?

Range("M" & i) is on sheet ??
Range("I" & i) is on sheet ??
Range("AA1") is on sheet ??
 
Upvote 0
Try

Code:
With Worksheets("Archer Search Report")
    .Range("M1").Value = "Days to Expiration"
    .Range("AA1").Value = Date
    FinalRow = .Cells(Rows.Count, 1).End(xlUp).Row
    For i = 2 To FinalRow
        .Range("M" & i).Value = .Range("I" & i).Value - .Range("AA1").Value
     Next i
End With
 
Last edited:
Upvote 0
I still got a type mismatch error. I did some digging on formatting and it worked after I added the following code. I suspected it had something to do with my column I format which this appears to correct:

Dim r As Range
Dim setdate As Range
Set setdate = Range(Cells(2, 9), Cells(2, 9).End(xlDown))
With setdate
.NumberFormat = "dd.mm.yyyy" 'I have the data in format "dd.mm.yy"
.Value = .Value
End With
 
Upvote 0
It sounds like you have/had some text dates in the column,
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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