The Day function in VBA (Visual Basic for Applications) is used to obtain the day part of a given date. It returns an integer value representing the day of the month from 1 to 31.
Basic Usage:
To use the Day function, simply pass a date value to it, and it will return the day part of that date.:VBA
Dim exampleDate As Date
Dim dayOfMonth As Integer
exampleDate = #2/15/2024# ' Example date
dayOfMonth = Day(exampleDate) ' Returns 15
Using with Now Function:
If you want to get the current day of the month, you can use the Now function which returns the current system date and time.VBA
Dim currentDay As Integer
currentDay = Day(Now) ' Returns the current day of the month
Working with Variables
VBA
Dim myDate As Date
Dim theDay As Integer
myDate = DateSerial(2024, 1, 24) ' Setting a specific date
theDay = Day(myDate) ' Returns 24