anuradhagrewal
Board Regular
- Joined
- Dec 3, 2020
- Messages
- 90
- Office Version
- 2010
- Platform
- Windows
Hi
This is a calendar update which I have made
I am not able to update all the cells in real time when I input the value in the data worksheet against a given date.
I want the given event to get updated next to the date in the calendrer worksheet
Can you kindly please guide me what wrong with the script
This is a calendar update which I have made
I am not able to update all the cells in real time when I input the value in the data worksheet against a given date.
I want the given event to get updated next to the date in the calendrer worksheet
Can you kindly please guide me what wrong with the script
VBA Code:
Sub UpdateCalendarQwen()
Dim dataSheet As Worksheet
Dim calendarSheet As Worksheet
Dim lastRow As Long
Dim i As Long
Dim eventDate As Date
Dim eventText As String
Dim monthName As String
Dim dayNumber As Integer
Dim foundMonth As Range
Dim foundDay As Range
Dim searchRange As Range
Dim monthRow As Long
' Set worksheets
Set dataSheet = ThisWorkbook.Sheets("Data")
Set calendarSheet = ThisWorkbook.Sheets("Calendar1")
' Find the last row in the Data sheet
lastRow = dataSheet.Cells(dataSheet.Rows.Count, 2).End(xlUp).Row
' Loop through each row in the Data sheet
For i = 2 To lastRow ' Assuming row 1 contains headers
On Error Resume Next ' Skip errors for invalid dates
eventDate = dataSheet.Cells(i, 2).Value ' Column B: Date
On Error GoTo 0
' Skip if date is invalid
If IsDate(eventDate) Then
eventText = dataSheet.Cells(i, 3).Value ' Column C: Event
' Skip if no event is entered
If Trim(eventText) <> "" Then
monthName = Format(eventDate, "MMMM") ' Get month name (e.g., "April")
dayNumber = day(eventDate) ' Get day number
Debug.Print "Processing date: " & eventDate & ", Month: " & monthName & ", Day: " & dayNumber
' Find the month header in Calendar1 sheet
Set foundMonth = calendarSheet.Cells.Find(What:=monthName, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
MatchCase:=False, _
SearchFormat:=False)
If foundMonth Is Nothing Then
Debug.Print "Month not found: " & monthName
' Try finding with leading/trailing spaces
Set foundMonth = calendarSheet.Cells.Find(What:=Trim(monthName), _
LookIn:=xlValues, _
LookAt:=xlPart, _
MatchCase:=False)
If Not foundMonth Is Nothing Then
Debug.Print "Found month with partial match: " & monthName
End If
Else
Debug.Print "Found month: " & monthName & " at " & foundMonth.Address
' Define search range from month row to next month or end of sheet
monthRow = foundMonth.Row
Dim nextMonth As Range
Set nextMonth = calendarSheet.Cells.FindNext(After:=foundMonth)
Dim endRow As Long
If Not nextMonth Is Nothing And nextMonth.Row > monthRow Then
endRow = nextMonth.Row - 1
Else
endRow = calendarSheet.UsedRange.Rows.Count
End If
' Search for day number in the month's section
Set foundDay = calendarSheet.Range(calendarSheet.Cells(monthRow + 1, 1), _
calendarSheet.Cells(endRow, 31)) _
.Find(What:=dayNumber, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows)
If foundDay Is Nothing Then
' Try finding day as text
Set foundDay = calendarSheet.Range(calendarSheet.Cells(monthRow + 1, 1), _
calendarSheet.Cells(endRow, 31)) _
.Find(What:=CStr(dayNumber), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows)
Debug.Print "Day not found as number, trying as text: " & dayNumber
End If
If foundDay Is Nothing Then
Debug.Print "Day not found: " & dayNumber & " in month " & monthName
Else
Debug.Print "Found day: " & dayNumber & " at " & foundDay.Address
' Write event text below the day number
calendarSheet.Cells(foundDay.Row + 1, foundDay.Column).Value = eventText
End If
End If
Else
Debug.Print "Empty event text skipped for date: " & eventDate
End If
Else
Debug.Print "Invalid date skipped: " & dataSheet.Cells(i, 2).Value
End If
Next i
MsgBox "Calendar updated successfully!"
End Sub