如何在 VBA 中设置标签标题?

How to get a label caption to be set in VBA?

这是我第一次创建用户窗体。 我一直在寻找解决方案,但尚未找到有效的解决方案。

我正在尝试为几个标签设置标题。我希望在打开表单时设置它们。

我现在有这个(在用户表单代码中:

Private Sub Budget_Initialize()
    Me.Label25.Caption = Format(Month(Now), "mmmm")
    Me.Label26.Caption = Format(Month(Now) + 1, "mmmm")
    Me.Caption = "Test, Test, Test!"
    Me.Repaint
End Sub

但是没用。

我也试过了(在 ThisWorkbook 代码中):

Private Sub Workbook_Open()
    Budget.Show
    With Budget
        .Label25.Caption = Format(Month(Now), "mmmm")
        .Label26.Caption = Format(Month(Now) + 1, "mmmm")
        .Caption = "Test, Test, Test!"
    End With
    Budget.Repaint
End Sub

它会在我打开工作簿时打开用户窗体,但不会更新标题。

我希望标题是本月和下个月的名称。

我做错了什么?

你需要的事件例程(与窗体名称无关)

Private Sub UserForm_Initialize()
    ....
End Sub

基本上,只需将 Budget_Initialize() 重命名为 UserForm_Initialize()

Format(Month(Now), "mmmm") 将始终为一月,因为它最大为 Format(12, "mmmm")。跳过一个月,现在一个人就是你需要的。