【问题标题】:VBA Dynamic userform gives "Object variable or With block variable not set" errorVBA 动态用户窗体给出“对象变量或未设置块变量”错误
【发布时间】:2018-02-18 00:42:21
【问题描述】:

下面是我尝试动态创建一个框架,为其添加(动态创建的)标签,然后将所述框架添加到我的用户表单中的另一个框架。

但在 with 部分它给出错误 91“对象变量或未设置块变量”

我做错了什么?在保持干净的现代/高效代码的同时,真的很难做更高级的 vba 逻辑。

Dim newf As MSForms.Frame

'First the description
With newf.Controls.Add("Forms.Label.1")
    'Formating it
    .SpecialEffect = fmSpecialEffectEtched
    .Caption = ComboBox1.Value
    .Height = 20
    .Width = ComboBox1.Width - 10
    .Left = 10
    newp.Font.Size = 12
End With
'FrameProduct already pre exists
Me.FrameProducts.Controls.Add newf

【问题讨论】:

  • 您已声明 newf 是一个 msforms.frame,但您实际上并未使用 set newf = ... 命令创建一个

标签: vba excel


【解决方案1】:

原来我只是设置变量,而不是创建对象,似乎创建控件的唯一方法是我需要调用 .controls.Add 方法,所以解决方案最终是这些光滑的嵌套 withs

Dim newf As MSForms.Frame
Set newf = Me.FProducts.Controls.Add("Forms.Frame.1")
With newf
    .Visible = False
    'First the description
    With .Controls.Add("Forms.Label.1")
        .SpecialEffect = fmSpecialEffectEtched
        .Caption = ComboBox1.Value
        .Height = 20
        .Width = ComboBox1.Width - 10
        .Left = 10
        .Font.Size = 12
    End With
    .Visible = True
End with

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多