Number of the days in a month - vba

pauloalex

New Member
Joined
Jan 16, 2021
Messages
6
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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
14,572
Office Version
  1. 365
Platform
  1. Windows
Try
VBA Code:
Dim dias_do_mes As Long
dias_do_mes = Evaluate("=Day(EoMonth(Date(" & Range("K3").Value & "," & Range("J3").Value & ",1),0))")
 
Solution

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,194
Office Version
  1. 2013
Platform
  1. Windows
TRy this
VBA Code:
 nb_days = Day(DateSerial(Range("K3").Value, Range("J3").Value + 1, 1) - 1)
 

Dave Patton

Well-known Member
Joined
Feb 15, 2002
Messages
5,031
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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))
 

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
14,572
Office Version
  1. 365
Platform
  1. Windows
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.
 

Forum statistics

Threads
1,148,394
Messages
5,746,443
Members
424,020
Latest member
LongDoo

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
Top