Foglio1
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.NumberFormat = "m/d/yyyy" Then
UserForm1.Show
End If
End Sub
----------------------------------------------------------------------------
UserForm1
Dim WithEvents CmdArray As cCtlMatrix
Private Sub UserForm_Initialize()
Dim ctl As Control
Set CmdArray = New cCtlMatrix
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CommandButton Then
CmdArray.Add ctl
End If
Next ctl
End Sub
Private Sub UserForm_Activate()
Dim ctl As Control
Me.dToDay = Format(Date, "mmmm yyyy")
Me.dToDay.TextAlign = fmTextAlignCenter
Me.DataSel = Date
Me.DataSel.TextAlign = fmTextAlignCenter
Call CreaCal
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CommandButton Then
If ctl.Caption = CStr(Day(DataSel)) And ctl.Visible = True Then
ctl.FontBold = True
ctl.ForeColor = vbRed
ctl.BackColor = RGB(255, 235, 205)
End If
End If
Next ctl
End Sub
Private Function CreaCal()
Dim ctl As Control
' carica il calendario
Dim curday As Variant, curbox As Integer
curday = DateSerial(Year(Me![dToDay]), Month(Me![dToDay]), 1)
curday = DateAdd("d", 1 - Weekday(curday, vbMonday), curday)
For curbox = 0 To 41
Me("D" & curbox).Caption = Day(curday)
Me("D" & curbox).Visible = False
If Month(curday) = Month(Me!dToDay) Then Me("D" & curbox).Visible = True
Me("D" & curbox).Font.Size = 7
curday = curday + 1
Next curbox
End Function
Private Sub SpinButton1_SpinUp()
Dim ctl As Control
' sposta avanti i mesi
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CommandButton Then
ctl.FontBold = False
ctl.ForeColor = vbBlack
ctl.BackColor = &H8000000F
End If
Next ctl
Me.DataSel = ""
Me!dToDay = DateAdd("m", 1, dToDay)
Me.dToDay = Format(Me.dToDay, "mmmm yyyy")
Call CreaCal
End Sub
Private Sub SpinButton1_SpinDown()
' sposta in dietro i mesi
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CommandButton Then
ctl.FontBold = False
ctl.ForeColor = vbBlack
ctl.BackColor = &H8000000F
End If
Next ctl
Me.DataSel = ""
Me!dToDay = DateAdd("m", -1, dToDay)
Me.dToDay = Format(Me.dToDay, "mmmm yyyy")
Call CreaCal
End Sub
Private Sub CmdArray_Click(item As Object)
Dim selday As Integer, DateSelected As Variant, ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CommandButton And ctl.Visible = True Then
ctl.FontBold = False
ctl.ForeColor = vbBlack
ctl.BackColor = &H8000000F
End If
Next ctl
Me(item.Name).FontBold = True
Me(item.Name).ForeColor = vbRed
Me(item.Name).BackColor = RGB(255, 235, 205)
selday = Val(Me(item.Name).Caption)
Me.DataSel = DateSerial(Year(Me![dToDay]), Month(Me![dToDay]), selday)
End Sub
Private Sub CommandButton1_Click()
ActiveCell = Me.DataSel
UserForm1.Hide
End Sub
----------------------------------------------------------------------------------------
cCtlMatrix
Dim itm As cMatrixItem
Dim cControls As Collection
Public Event Click(item As Object)
Private Sub Class_Initialize()
Set cControls = New Collection
End Sub
Public Sub Add(ByVal actItm As Object)
Set itm = New cMatrixItem
Set itm.CallerObject = Me
If TypeOf actItm Is MSForms.CommandButton Then
Set itm.itmCmd = actItm
End If
cControls.Add itm
Set itm = Nothing
End Sub
Public Property Get Count() As Single
Count = cControls.Count
End Property
Public Property Get ItemCollection(ByVal Index As Single) As Object
Dim tmpObject As Object
Set tmpObject = cControls(Index)
With tmpObject
If Not .itmCmd Is Nothing Then Set ItemCollection = .itmCmd
End With
End Property
Friend Sub ItemClick(item As Object)
RaiseEvent Click(item)
End Sub
------------------------------------------------------------------------------
cMatrixItem
Public WithEvents itmCmd As MSForms.CommandButton
Public CallerObject As cCtlMatrix
Private Sub itmCmd_Click()
CallerObject.ItemClick itmCmd
End Sub
|