Creating a Calendar for a Month with tasks

anuradhagrewal

Board Regular
Joined
Dec 3, 2020
Messages
87
Office Version
  1. 2010
Platform
  1. Windows
Hi
I have prepared a calendar file
What I am looking is at that if I enter anything in the worksheet Main under TASK (Column D) then the value of the task should appear in Calender.
The worksheet "Sample" is how I want the layout to be
So lets say in worksheet Main in C2 having date 01-04-2024 I enter a task in the corresponding column D2 then the VB code should pick this up and using the value of B2 in worksheet main which the day of the week.
It should make a new calendar worksheet named Calendar for the Month of (taking the value of the first day of the month as defined in worksheet Main C2 so in this case Calender for the Month of April)
As the month changes in column C of worksheet main a new worksheet should be made only for that month using the sample worksheet again and the whole process be repeated for the next month and so on.
Then using the sample layout enter the value of worksheet Main D2 in the corresponding cells next the value of the day of the week and below that enter the date in dd-mmm-yyyy format.
I tried this code but its not working

VBA Code:
Sub UpdateCalendar()
    Dim wsMain As Worksheet
    Dim taskDate As Date
    Dim task As String
    Dim calendarMonth As String
    Dim calendarYear As String
    Dim calendarSheet As Worksheet
    Dim lastRow As Long

    ' Set reference to the Main worksheet
    Set wsMain = ThisWorkbook.Sheets("Main")

    ' Retrieve task details from the Main worksheet
    taskDate = wsMain.Range("C2").Value
    task = wsMain.Range("D2").Value
    
    ' Get the year and month from the task date
    calendarYear = Year(taskDate)
    calendarMonth = Format(taskDate, "mmmm")

    ' Check if Calendar sheet for the month exists, if not create it
    On Error Resume Next
    Set calendarSheet = ThisWorkbook.Sheets("Calendar - " & calendarMonth & " " & calendarYear)
    On Error GoTo 0
    
    If calendarSheet Is Nothing Then
        ' Create a new worksheet for the calendar for the specific month
        Set calendarSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
        calendarSheet.Name = "Calendar - " & calendarMonth & " " & calendarYear
        ' Apply the Sample layout to the new calendar sheet
        
        ' Assuming the Sample layout is in a sheet called "Sample"
        ThisWorkbook.Sheets("Sample").UsedRange.Copy calendarSheet.Range("A1")
    End If
    
    ' Find the first empty row in the calendar sheet and enter the task details
    lastRow = calendarSheet.Cells(calendarSheet.Rows.Count, 1).End(xlUp).Row + 1
    calendarSheet.Cells(lastRow, 1).Value = task
    calendarSheet.Cells(lastRow, 2).Value = Format(taskDate, "dddd")
    calendarSheet.Cells(lastRow, 3).Value = Format(taskDate, "dd-mmm-yyyy")
End Sub

Thanks

Anuradha
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,223,881
Messages
6,175,161
Members
452,615
Latest member
bogeys2birdies

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