- 帖子
- 13
- 主題
- 0
- 精華
- 0
- 積分
- 63
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- office 365
- 閱讀權限
- 20
- 性別
- 女
- 註冊時間
- 2022-2-1
- 最後登錄
- 2024-11-20
|
本帖最後由 lee88 於 2022-12-26 15:17 編輯
回復 6# wsx1130
整理一下程式碼
UserForm1的程式碼- Option Explicit
- Private Sub UserForm_Initialize()
- With TextBox1
- .Width = 100
- .Font.Size = 12
- .Font.Bold = True
- End With
- End Sub
- Private Sub UserForm_Activate()
- If UserForm2.Visible = False Then MsgBox "按下表單 呼叫 日期表單"
- End Sub
- Private Sub UserForm_Click()
- If UserForm2.Visible = False Then UserForm2.Show 0
- End Sub
- Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
- If UserForm2.Visible = True Then Unload UserForm2
- End Sub
複製代碼 UserForm1的程式碼- Option Explicit
- Public Thisday As Date
- Dim CreateCal As Boolean, i As Integer
- Dim Form_Button(1 To 42) As New Class1 '新的物件模組
- Private Sub UserForm_Initialize()
- Thisday = Date
- For i = 1 To 12
- CB_Mth.AddItem Format(DateSerial(Year(Thisday), i, 1), "m月 mmmm")
- Next
- CB_Mth.Value = CB_Mth.List(Month(Thisday) - 1)
- For i = 1980 To 2099 ' PC內建日期年度
- CB_Yr.AddItem i
- Next
- CB_Yr.Value = Year(Thisday)
- Make_com
- CreateCal = True
- Call Build_Calendar
- Application.EnableEvents = True
- End Sub
- Sub Make_com()
- For i = 1 To 42
- Set Form_Button(i).C_Button = Controls("d" & i) '將表單的控制項 導入為物件模組的控制項
- Next
- End Sub
- Private Sub CB_Mth_Change()
- 'rebuilds the calendar when the month is changed by the user
- Build_Calendar
- End Sub
- Private Sub CB_Yr_Change()
- 'rebuilds the calendar when the year is changed by the user
- Build_Calendar
- End Sub
- Private Sub Build_Calendar()
- 'the routine that actually builds the calendar each time
- Dim S_Day As Date, Msg As Boolean
- If CreateCal = False Then Exit Sub
- UserForm2.Caption = " " & CB_Mth.Value & " " & CB_Yr.Value
- S_Day = DateSerial(CB_Yr, CB_Mth.ListIndex + 1, 1) - Weekday(DateSerial(CB_Yr, CB_Mth.ListIndex + 1, 1), vbMonday)
- 'S_Day = 42個按鍵的第一天
- For i = 1 To 42
- Msg = Month(S_Day + i - 1) = CB_Mth.ListIndex + 1
- With Controls("d" & i)
- .ControlTipText = S_Day + i - 1
- .Caption = Day(S_Day + i - 1) '指定日期
- .Font.Bold = Msg ' True '<-定粗體
- .ForeColor = IIf(Msg, vbBlack, vbWhite) 'Fc '字體顏色
- .ForeColor = IIf(Weekday(.ControlTipText) > 6 Or Weekday(.ControlTipText) = 1, vbRed, IIf(Msg, vbBlack, vbWhite))
- .BackColor = IIf(Msg, vbCyan, vbBlack) '背景顏色
- End With
- Next
- End Sub
- Private Sub UserForm_RemoveControl(ByVal Control As MSForms.Control)
- Unload Me
- End Sub
複製代碼 **須在專案視窗加上一個 物件類別模組 會自動命名為Class1(名稱可修改)
Class1的程式碼- Option Explicit
- Public WithEvents C_Button As MSForms.CommandButton
- Private Sub C_Button_Click()
- With C_Button.Parent.Parent.Parent
- ' 42個按鍵的.Parent = Frame1->Frame1.Parent=.Frame2->Frame2.Parent=UserForm2
- .Thisday = C_Button.ControlTipText
- UserForm1.TextBox1 = .Thisday
- End With
- End Sub
複製代碼 |
|