Number of the days in a month - vba

pauloalex

New Member
Joined
Jan 16, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello, everybody!

I'm trying to catch the number of the days in a given month with a variable. To do that, I'm using the follow line:

VBA Code:
dias_do_mes = Day(Application.WorksheetFunction.EoMonth(Application.WorksheetFunction.Date(Range("K3").Value, Range("J3").Value, 1), 0))

Month.xlsm
JK
2MonthYear
312021
Planilha1


But I'm getting the follow message:

1610879467671.png


Do somebody know how to solve it?

Thanks in advance!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try
VBA Code:
Dim dias_do_mes As Long
dias_do_mes = Evaluate("=Day(EoMonth(Date(" & Range("K3").Value & "," & Range("J3").Value & ",1),0))")
 
Upvote 0
Solution
TRy this
VBA Code:
 nb_days = Day(DateSerial(Range("K3").Value, Range("J3").Value + 1, 1) - 1)
 
Upvote 0
Either of the following will yield the number of days

T202101b.xlsm
JKLM
3120213131
4
3a
Cell Formulas
RangeFormula
L3L3=DAY(EOMONTH(DATE(K3,J3,1),0))
M3M3=DAY(DATE(K3,J3+1,0))
 
Upvote 0
Taking the DateSerial function that @mohadin used (which I had forgotten about), borrowing one of @Dave Patton's formulas, and throwing in a bit of shorthand.
VBA Code:
dias_do_mes = day(dateserial([K3],[J3]+1,0))
I'm sure there are many other ways as well although some would be less practical, using Format instead of Day is one such alternative that comes to mind.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,390
Members
448,957
Latest member
Hat4Life

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