VBA Leap Year/Non-Leap Year Calculation

johnston

New Member
Joined
Mar 14, 2018
Messages
49
I'm making a table with all the dates in a month. The user picks month and year. I have just the code listed below that calculates leap year for February and all other months of any year. The problem is that it does not calculate February during a non-leap year. The Application-defined or object-defined error message comes up during the autofill part of my code. I bolded the code that triggers the error message. Anything helps!



If monthNum=11 Or monthNum=4 Or monthNum=9 Or monthNum= 6 Then
numDays=30

ElseIf monthNum=2 Then
If year Mod 4 =0 Then
If year Mod 100= 0 Then
If year Mod 400= Then
numDays= 28
Else
numDays =29
End If

numDays=28
Else
numDays=29
End If
End If
Else
numDays=31
End If

Range("A6")=DateSerial(year, monthNum, 1)
Range("A6").Autofill Destination:=Range("A6").Resize(numDays, 1), Type:=xlFillDefault
 
Thank you, this is getting me the correct number of days without extra dates, but when its formatted to Short Date it makes the monthNum and yearNum I entered a date like 1/1/1900 even though I entered monthNum as 2 and yearNum as 2018
Ah, you wanted the actual date rather than just the day number. Okay, try these two lines of code instead...
Code:
[table="width: 500"]
[tr]
	[td]  NumDays = Day(DateSerial(YearNum, MonthNum + 1, 0))
  Range("A6").Resize(NumDays) = Evaluate("IF({1},DATE(" & YearNum & "," & MonthNum & ",ROW(1:" & NumDays & ")))")[/td]
[/tr]
[/table]
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Ah, you wanted the actual date rather than just the day number. Okay, try these two lines of code instead...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]  NumDays = Day(DateSerial(YearNum, MonthNum + 1, 0))
  Range("A6").Resize(NumDays) = Evaluate("IF({1},DATE(" & YearNum & "," & MonthNum & ",ROW(1:" & NumDays & ")))")[/TD]
[/TR]
</tbody>[/TABLE]

This is really close. When I run the macro the date comes up in the General Number format, but when I apply the Short Date format after running the macro it comes out correctly. Is there a way to make it a Short Date format and applying it to a range?
Example:
43863 in Number Format
2/2/2020 in Short Date
 
Upvote 0
This is really close. When I run the macro the date comes up in the General Number format, but when I apply the Short Date format after running the macro it comes out correctly. Is there a way to make it a Short Date format and applying it to a range?
Example:
43863 in Number Format
2/2/2020 in Short Date
Maybe add this line of code after the ones I posted...
Code:
Range("A6").Resize(NumDays).NumberFormat = "m/d/yyyy"
 
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