Lil Stinker
Board Regular
- Joined
- Feb 16, 2022
- Messages
- 151
- Office Version
- 2019
- Platform
- Windows
Hey folks,
I'm hoping you folks can provide some expertise here. I am designing a monthly calendar system in Excel. The code I have works well to enter the event time and title in a rectangular shape and place that shape in the appropriate matching date column on the calendar. The issue I'm having is I want to limit the number of shapes on any given date to 5 and have an additional shape appear to indicate if there are more than 5 events on that date, click here to see all events, essentially. The problem with the existing code is even with a Long variable setting the Daily Event Count to 5 (dayevntCOUNT), it continues creating the shapes for every event logged for that date and piles them on top of each other which messes up the time-of-day order displayed. The same occurs to the additional shape, the "more" button, which gets replicated for the same number of events there are on that date.
I didn't write the code, I've only tweaked it to include adding a "more" button. I've tried jumping out of the For loop in several locations in the code but nothing seems to limit the event shapes to 5 and the more shape to 1. I'm stumped so any help would be appreciated.
I'm hoping you folks can provide some expertise here. I am designing a monthly calendar system in Excel. The code I have works well to enter the event time and title in a rectangular shape and place that shape in the appropriate matching date column on the calendar. The issue I'm having is I want to limit the number of shapes on any given date to 5 and have an additional shape appear to indicate if there are more than 5 events on that date, click here to see all events, essentially. The problem with the existing code is even with a Long variable setting the Daily Event Count to 5 (dayevntCOUNT), it continues creating the shapes for every event logged for that date and piles them on top of each other which messes up the time-of-day order displayed. The same occurs to the additional shape, the "more" button, which gets replicated for the same number of events there are on that date.
I didn't write the code, I've only tweaked it to include adding a "more" button. I've tried jumping out of the For loop in several locations in the code but nothing seems to limit the event shapes to 5 and the more shape to 1. I'm stumped so any help would be appreciated.
VBA Code:
Sub CALrefresh()
'''clear all existing events from month schedule'''
For Each evntSHP In Schedule.Shapes
If InStr(evntSHP.Name, "CALevnt") > 0 Then evntSHP.Delete
Next evntSHP
'''refresh calendar with daily events sorted by date & time from schedule record'''
evntNUM = 1 'set default event # to 1
With SCHrecord
lastROW = .Range("A1048576").End(xlUp).Row 'last item row
If lastROW < 3 Then Exit Sub
Application.ScreenUpdating = False
.Range("A2:Q" & lastROW).AdvancedFilter xlFilterCopy, CriteriaRange:=.Range("W1:X2"), CopyToRange:=.Range("AA2:AQ2"), Unique:=True
lastresROW = .Range("AA99999").End(xlUp).Row 'last result row
If lastresROW < 3 Then Exit Sub
If lastresROW < 4 Then GoTo SkipSort
With .Sort
.SortFields.Clear
.SortFields.Add Key:=SCHrecord.Range("AC3"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'sort based on start date
.SortFields.Add Key:=SCHrecord.Range("AI3"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal 'then sort based on all day event
.SortFields.Add Key:=SCHrecord.Range("AE3"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'then sort based on start time
.SetRange SCHrecord.Range("AA3:AQ" & lastresROW) 'set result range
.Apply
End With
SkipSort:
For resROW = 3 To lastresROW
evntID = .Range("AA" & resROW).Value 'event id
evntNAME = .Range("AB" & resROW).Value 'event title
evntDATE = .Range("AC" & resROW).Value 'event start date
evntTIME = Format(.Range("AE" & resROW).Value, "h:mma/p") 'event time formatted
evntCAT = .Range("AJ" & resROW).Value 'category
evntCOLOR = Schedule.Range("AM2").Interior.Color 'shape color
dayevntCOUNT = Application.WorksheetFunction.CountIf([CALstdayresults], evntDATE) 'get number of events on single day
If dayevntCOUNT > 5 Then dayevntCOUNT = 5 'set event limit to 5
'''create event shapes and position on matching dates'''
For calROW = 9 To 39 Step 6
For calCOL = 4 To 16 Step 1
If Schedule.Cells(calROW, calCOL).Value = evntDATE Then 'day found
Schedule.Shapes("CALeventsample").Duplicate.Name = "CALevnt" & evntID
With Schedule.Shapes("CALevnt" & evntID)
.Left = Schedule.Cells(calROW, calCOL).Left + 1 'left position
.Top = Schedule.Cells(calROW - 6 + evntNUM, calCOL).Top + 1
.Width = Schedule.Cells(calROW + evntNUM, calCOL + 1).Width + 28
.Height = Schedule.Cells(calROW + evntNUM, calCOL).Height - 4
.TextFrame2.TextRange.Text = evntTIME & " | " & evntNAME 'text inside shape
.Fill.ForeColor.RGB = evntCOLOR 'set event color
End With
If dayevntCOUNT > 5 And Schedule.Cells(calROW, calCOL).Value = evntDATE Then Schedule.Shapes("CALeventmore").Duplicate.Name = "CALevntMR"
On Error Resume Next
With Schedule.Shapes("CALevntMR")
.Left = Schedule.Cells(calROW, calCOL + 1).Left + 80 'left position
.Top = Schedule.Cells(calROW, calCOL).Top - 5 'top position
End With
On Error GoTo 0
If evntNUM >= dayevntCOUNT Then
If evntNUM > 5 Then GoTo NextDay
evntNUM = 1
Else
evntNUM = evntNUM + 1 'increment by 1
End If
End If
Next calCOL
Next calROW
NextDay:
Next resROW
End With
Application.ScreenUpdating = True
End Sub