无法将类模块中的变量写入文本框

时间:2018-02-23 20:17:01

标签: excel-vba vba excel

我在Userform上的框架中有一个日历(由标签组成)。我从类模块中的sub运行每个标签上的click事件代码(表示日历上的日期)。我从标签中取出值并将其复制到文本框中。这一切似乎都有效,没有任何错误,但价值不复制。

我定义变量:

   Public Const iStartWeekday  As Integer = vbSunday
   Public iYear As Integer
   Public iMonth As Integer
   Public dtCalendar As Date
   Public DateLabels() As New clsDateLabels
   Public dtFirstOfMonth As Date
   Public dtLastOfMonth As Date
   Public iDay As Integer
   Public iWeekday As Integer
   Public dtWeekdayStart As Date

这是在userform的初始化事件中:

Public Sub MakeCal()
dtFirstOfMonth = DateSerial(iYear, iMonth, 1) dtLastOfMonth = DateSerial(iYear, iMonth + 1, 0) With Me
    .Caption = sFormName
    .sbMonth.Value = iMonth
    .sbYear.Value = iYear
    On Error GoTo 0
    For iDay = 1 To 42
        With .Controls("lblDay" & iDay)
            If WorksheetFunction.Weekday(dtFirstOfMonth, iStartWeekday) - iDay >= 1 Or Day(dtLastOfMonth) < WorksheetFunction.Sum(iDay - WorksheetFunction.Weekday(dtFirstOfMonth, iStartWeekday)) + 1 Then
                .Visible = False
            Else
                .Visible = True
                .Caption = WorksheetFunction.Sum(iDay - WorksheetFunction.Weekday(dtFirstOfMonth, iStartWeekday)) + 1
                If iDay = Day(Now) + WorksheetFunction.Weekday(dtFirstOfMonth, iStartWeekday) - 1 And iYear
= Year(Date) And iMonth = Month(Date) Then
                    .BackColor = RGB(205, 231, 247)
                Else
                    .BackColor = vbWhite
                End If
            End If
        End With
        ReDim Preserve DateLabels(1 To iDay)
        Set DateLabels(iDay).DateLabels = .Controls("lblDay" & iDay)
    Next iDay
    .lblMonthandYear.Caption = Format(DateSerial(iYear, iMonth, 1), "mmm yyyy")
    dtWeekdayStart = #1/1/2001#
    For iWeekday = 1 To 7
        If iStartWeekday = vbSunday Then
            .Controls("lblWeekday" & iWeekday).Caption = Left(Format(dtWeekdayStart + iWeekday - 2, "ddd"), 2)
        Else
            .Controls("lblWeekday" & iWeekday).Caption = Left(Format(dtWeekdayStart + iWeekday - 1, "ddd"), 2)
        End If
    Next iWeekday

结束 结束子

这是类模块上的sub:

    Option Explicit

    Public WithEvents DateLabels As MSForms.Label
    Public WithEvents DateStart As MSForms.ComboBox
    Public MyForm As String

Public Sub DateLabels_Click()
    dtCalendar = DateSerial(iYear, iMonth, DateLabels.Caption)
    EntryEdit.Controls(MyDateBox).Value = dtCalendar
    EntryEdit.FrameCal.Visible = False
End Sub

奇怪的是,它似乎都运行良好,但是值不会被复制到文本框中。如果我将日历复制到自己的表单上,它甚至可以工作。是否有一些明显我遗漏的东西可以解释为什么价值不能复制?

0 个答案:

没有答案