1 Attachment(s)
[RESOLVED] Add full month worth of data to worksheet
I have data for a whole month that looks like this. The data includes all the days in December:
Attachment 186448
I'd like a macro that would add similar data for January 2023.
The general rule is that when the macro is executed, that if there is existing data, the data for the next month is added, otherwise the current month is added.
Re: Add full month worth of data to worksheet
Haven’t understood your rule.
existing data of what? Current day?
Re: Add full month worth of data to worksheet
If the worksheet is empty except for the headings in rows 1 and 2, add the Day, Date and Time for all the days in the current month, otherwise add the Day, Date and Time for all the days in the next month.
For example if today's date is December 17th, 2022 and the worksheet is empty except for the headings in rows 1 and 2, add the Day, Date and Time for all the full month of December starting in row 3. Then when the macro is run again skip one row below the existing December data and add the Day, Date and Time for all the days in the January 2023.
Re: Add full month worth of data to worksheet
Just check if A3 is empty.
then it‘s a simple loop with Step 5
Re: Add full month worth of data to worksheet
It's really not that simple. Things to consider:
- How many days in the month?
- What is the correct Day for the Date?
- How to avoid adding a month 13?
- "Step 5" is problematic for the last week in a month.
Re: Add full month worth of data to worksheet
1) use DateAdd to get „LastDayOfMonth“. search for this. Gazillion examples
2) look at Format-Function. Or was it „weekday“? *hint*
3) use DateAdd
4) it‘s not. The step 5 is for the block of rows. And don‘t use a For-Loop
Re: Add full month worth of data to worksheet
This seems straightforward. The only thing that seems to be changing is the day of the month - for which a simple loop would work. Date column and day of week column have the same values - you just change the formatting (D-MMM-YY v DDD). And you can get the days of the month through the EOMONTH function:
Code:
Function DaysInMonth(ByVal MonthValue As Long, Optional YearValue As Long = -1)
If YearValue = -1 Then YearValue = Year(Now)
DaysInMonth = Day(WorksheetFunction.EoMonth(DateSerial(YearValue, MonthValue, 1), 0))
End Function
Re: Add full month worth of data to worksheet
Thanks all. I worked it out.