VBAdynamic用户表单给出“对象variables或带块variables未设置”错误

贝娄是我尝试dynamic创build一个框架,添加(dynamic创build的)标签,然后将所述框架添加到我的用户窗体中的另一个框架。

但在部分它给出了错误91“对象variables或块variables未设置”

我做错了什么? 真正努力做到更高级的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 

原来我只是设置variables,而不是创build对象,似乎创build一个控制的唯一方法我需要调用.controls.Add方法,所以解决scheme结束了这些光滑嵌套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