我有一个在运行时创建命令按钮的模块。它将以指定的用户表单创建命令按钮。当我执行模块时,程序运行良好。
但是,当我使用用户表单来调用模块时,出现错误说明
Run-time error '91':
Object variable or With block variable not set
代码
Sub AddButtonAndShow()
Dim Butn As CommandButton
Dim Line As Long
Dim objForm As Object
Dim i As Integer
Dim x As Integer
Set objForm = ThisWorkbook.VBProject.VBComponents("Current_Stock")
For i = 1 To 3
Set Butn = objForm.Designer.Controls.Add("Forms.CommandButton.1")
With Butn
.Name = "CommandButton" & i
.Caption = i
.Width = 100
.Left = 300
.Top = 10 * i * 2
End With
Next
For x = 1 To 3
With objForm.CodeModule
Line = .CountOfLines
.InsertLines Line + 1, "Sub CommandButton" & x & "_Click()"
.InsertLines Line + 2, "MsgBox ""Hello!"""
.InsertLines Line + 3, "End Sub"
End With
'
Next x
End Sub
好心提醒。