返回列表 上一主題 發帖

各位大大,請問UserForm表單如何讀取子表單

本帖最後由 lee88 於 2022-12-26 15:17 編輯

回復 6# wsx1130
整理一下程式碼
UserForm1的程式碼
  1. Option Explicit
  2. Private Sub UserForm_Initialize()
  3.     With TextBox1
  4.         .Width = 100
  5.         .Font.Size = 12
  6.         .Font.Bold = True
  7.     End With
  8. End Sub
  9. Private Sub UserForm_Activate()
  10.   If UserForm2.Visible = False Then MsgBox "按下表單  呼叫 日期表單"
  11. End Sub
  12. Private Sub UserForm_Click()
  13.   If UserForm2.Visible = False Then UserForm2.Show 0
  14. End Sub
  15. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  16.     If UserForm2.Visible = True Then Unload UserForm2
  17. End Sub
複製代碼
UserForm1的程式碼
  1.   Option Explicit
  2.     Public Thisday As Date
  3.     Dim CreateCal As Boolean, i As Integer
  4.     Dim Form_Button(1 To 42) As New Class1 '新的物件模組
  5. Private Sub UserForm_Initialize()
  6.     Thisday = Date
  7.     For i = 1 To 12
  8.         CB_Mth.AddItem Format(DateSerial(Year(Thisday), i, 1), "m月  mmmm")
  9.     Next
  10.     CB_Mth.Value = CB_Mth.List(Month(Thisday) - 1)
  11.     For i = 1980 To 2099 ' PC內建日期年度
  12.         CB_Yr.AddItem i
  13.     Next
  14.     CB_Yr.Value = Year(Thisday)
  15.     Make_com
  16.     CreateCal = True
  17.     Call Build_Calendar
  18.     Application.EnableEvents = True
  19. End Sub
  20. Sub Make_com()
  21.     For i = 1 To 42
  22.        Set Form_Button(i).C_Button = Controls("d" & i)   '將表單的控制項 導入為物件模組的控制項
  23.     Next
  24. End Sub
  25. Private Sub CB_Mth_Change()
  26.     'rebuilds the calendar when the month is changed by the user
  27.     Build_Calendar
  28. End Sub
  29. Private Sub CB_Yr_Change()
  30.     'rebuilds the calendar when the year is changed by the user
  31.     Build_Calendar
  32. End Sub
  33. Private Sub Build_Calendar()
  34.     'the routine that actually builds the calendar each time
  35.     Dim S_Day As Date, Msg As Boolean
  36.     If CreateCal = False Then Exit Sub
  37.     UserForm2.Caption = " " & CB_Mth.Value & " " & CB_Yr.Value
  38.     S_Day = DateSerial(CB_Yr, CB_Mth.ListIndex + 1, 1) - Weekday(DateSerial(CB_Yr, CB_Mth.ListIndex + 1, 1), vbMonday)
  39.     'S_Day =  42個按鍵的第一天
  40.     For i = 1 To 42
  41.         Msg = Month(S_Day + i - 1) = CB_Mth.ListIndex + 1
  42.         With Controls("d" & i)
  43.                 .ControlTipText = S_Day + i - 1
  44.                 .Caption = Day(S_Day + i - 1)            '指定日期
  45.                 .Font.Bold = Msg ' True                    '<-定粗體
  46.                 .ForeColor = IIf(Msg, vbBlack, vbWhite) 'Fc '字體顏色
  47.                 .ForeColor = IIf(Weekday(.ControlTipText) > 6 Or Weekday(.ControlTipText) = 1, vbRed, IIf(Msg, vbBlack, vbWhite))
  48.                 .BackColor = IIf(Msg, vbCyan, vbBlack)              '背景顏色
  49.         End With
  50.     Next
  51. End Sub
  52. Private Sub UserForm_RemoveControl(ByVal Control As MSForms.Control)
  53.     Unload Me
  54. End Sub
複製代碼
**須在專案視窗加上一個 物件類別模組 會自動命名為Class1(名稱可修改)
Class1的程式碼
  1. Option Explicit
  2. Public WithEvents C_Button    As MSForms.CommandButton
  3. Private Sub C_Button_Click()
  4.         With C_Button.Parent.Parent.Parent
  5.             ' 42個按鍵的.Parent = Frame1->Frame1.Parent=.Frame2->Frame2.Parent=UserForm2
  6.             .Thisday = C_Button.ControlTipText
  7.             UserForm1.TextBox1 = .Thisday
  8.         End With
  9. End Sub
複製代碼

TOP

        靜思自在 : 能善用時間的人,必能掌握自己努力的方向。
返回列表 上一主題