Month function throwing type mismatch in VBA

kyddrivers

Board Regular
Joined
Mar 22, 2013
Messages
59
Office Version
  1. 365
Platform
  1. Windows
I have a file with 500+ rows of data. I would like to write a script that will loop thru and do 1 calculation (ABC) if month(C2)-Month(D2) = 0 or do a different calculation (XYZ) if month(C2)-Month(D2) <> 0

Columns C & D are formatted as Date


IDWeekWeek StartWeek End
311/1/20231/7/2023ABC
351/29/20232/4/2023XYZ

VBA Code:
Sub Calc_Test ()
Dim Calc as Integer

Calc = Application.Evaluate("Month(R2C3)-Month(R2C4)")
' rest of the script goes here
End Sub

Each time I run the code, I get a Type 13 Mismatch at the calc variable. I have tried setting Calc as Long, getting the same error.

How do I assign the result of month(C2)-Month(D2) to the Calc variable so I can proceed from there?

Thanks!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Do you use R1C1 notation in your sheets, or A1 notation?
 
Upvote 0
Do you use R1C1 notation in your sheets, or A1 notation?
If I am working directly on a worksheet, I use A1; whenever I write a script, I always use R1C1. If you are asking if the R1C1 reference style is checked in formula options, the answer is no.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
How do I increment or offset the evaluate function?
VBA Code:
Calc = Application.Evaluate ("Month(D2)-Month(C2)")

I tried writing this, but the return is always 0

VBA Code:
Dim Calc As Integer
Dim WeekStart As Range
Dim WeekEnd As Range

Set WeekStart = Range("C2")
Set WeekEnd = Range("D2")

Calc = Application.Evaluate("Month(" & WeekEnd & ")" & "-Month(" & WeekStart & ")")

Set WeekStart = WeekStart.Offset(1, 0)
Set WeekEnd = WeekEnd.Offset(1, 0)

Thanks!
 
Upvote 0
If you are going to use evaluate, I think it is looking for the physical address of the cell.

So maybe try:
Rich (BB code):
Calc = Application.Evaluate("Month(" & WeekEnd.Address & ")" & "-Month(" & WeekStart.Address & ")")

However, all of that really seems unnecessary. VBA has a MONTH function too.
So if you are using range variables, you could just use this much simpler VBA formula instead:
VBA Code:
Calc = Month(WeekEnd) - Month(WeekStart)
 
Upvote 0

Forum statistics

Threads
1,215,355
Messages
6,124,468
Members
449,163
Latest member
kshealy

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