Vba 在UserForm上为动态创建的按钮添加代码时CreateEventProc失败
我一直在为我们目前从遗留系统中提取的数据开发各种报告和表格。我已经创建了一个表单,它可以动态创建按钮,并根据创建的按钮数量对按钮进行分隔。我的错误是,我试图为每个按钮添加_Click()功能,因为创建的每个按钮的代码都是唯一的。我已经尝试了我能想到的一切,我能在网上找到的一切,都没有用。通过各种不同的尝试,我在UserFormCodeModule中找到了按钮和填充的代码,但是_Click()事件不会从那里触发。任何帮助都将不胜感激Vba 在UserForm上为动态创建的按钮添加代码时CreateEventProc失败,vba,excel,Vba,Excel,我一直在为我们目前从遗留系统中提取的数据开发各种报告和表格。我已经创建了一个表单,它可以动态创建按钮,并根据创建的按钮数量对按钮进行分隔。我的错误是,我试图为每个按钮添加_Click()功能,因为创建的每个按钮的代码都是唯一的。我已经尝试了我能想到的一切,我能在网上找到的一切,都没有用。通过各种不同的尝试,我在UserFormCodeModule中找到了按钮和填充的代码,但是_Click()事件不会从那里触发。任何帮助都将不胜感激 Private Sub CommandButton5_Click
Private Sub CommandButton5_Click()
Dim lastrow As Long, i As Integer, numButtons As Integer, newButton As Control, lineNum As Long
numButtons = 1
With Sheets("Production Capacity")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A4:AD" & lastrow).Interior.Color = RGB(255, 255, 255)
For i = 4 To lastrow
If i Mod 4 = 0 Then
If .Cells(i, "D").Value > .Cells(2, "G").Value Then
.Cells(i, "G").Interior.Color = RGB(255, 0, 0)
Set newButton = Me.Controls.Add("Forms.CommandButton.1", "button" & numButtons, False)
With newButton
.Width = 200
Select Case (numButtons Mod 3)
Case 0
.Left = 475
Case 1
.Left = 25
Case 2
.Left = 250
End Select
.Visible = True
.Height = 20
.Top = 60 + (Int((numButtons - 1) / 3) * 40)
.Caption = Sheets("Production Capacity").Cells(i, "A").Value & " - " & Sheets("Production Capacity").Cells(i, "B").Value & " DeptName"
.Font.Size = 10
.Font.Bold = True
End With
With ActiveWorkbook.VBProject.VBComponents("Class1").CodeModule
lineNum = .CreateEventProc("Click", "button" & numButtons) + 1 'This line is where the error occurs.
.InsertLines lineNum, _
"Dim lastrow as Long" & Chr(13) & _
"with Sheets(Sheets(""Production Capacity"").cells(1, ""A"").value)" & Chr(13) & _
".ShowAllData" & Chr(13) & _
"lastrow = .Cells(Rows.Count, ""B"").End(xlUp).Row" & Chr(13) & _
".Range(""A$6:$BQ$"" & lastrow).AutoFilter field:=30, Criteria1:=" & Chr(34) & ">=" & Chr(34) & " & " & Chr(34) & DateValue(Sheets("Production Capacity").Cells(i, "A").Value) & Chr(34) & ", Operator:=xlAnd, Criteria2:=" & Chr(34) & "<=" & Chr(34) & " & " & Chr(34) & DateValue(Sheets("Production Capacity").Cells(i, "B").Value) & Chr(34) & ", Operator:=xlAnd" & Chr(13) & _
"End With"
End With
numButtons = numButtons + 1
End If
Private子命令按钮5_单击()
Dim lastrow为长,i为整数,numButtons为整数,newButton为控件,lineNum为长
numButtons=1
带板材(“生产能力”)
lastrow=.Cells(.Rows.Count,“A”).End(xlUp).Row
.Range(“A4:AD”和lastrow).Interior.Color=RGB(255、255、255)
对于i=4到最后一行
如果i Mod 4=0,则
如果.Cells(i,“D”).Value>则.Cells(2,“G”).Value
.Cells(i,“G”).Interior.Color=RGB(255,0,0)
Set newButton=Me.Controls.Add(“Forms.CommandButton.1”、“button”和numButtons,False)
纽扣
.宽度=200
选择案例(Numbertons Mod 3)
案例0
.左=475
案例1
.左=25
案例2
.左=250
结束选择
.Visible=True
.高度=20
.Top=60+(Int((numButtons-1)/3)*40)
.Caption=工作表(“生产能力”)。单元格(i,“A”)。数值和“-”和工作表(“生产能力”)。单元格(i,“B”)。数值和“部门名称”
.Font.Size=10
.Font.Bold=True
以
使用ActiveWorkbook.VBProject.VBComponents(“Class1”).CodeModule
lineNum=.CreateEventProc(“单击”、“按钮”和Numbertons)+1'这一行是发生错误的地方。
.InsertLines lineNum_
“与最后一行一样长”和Chr(13)以及_
“与表(表(““生产能力”)。单元格(1”,“A”)。值)”&Chr(13)和_
“.ShowAllData”&Chr(13)和_
“lastrow=.Cells(Rows.Count”,“B”).End(xlUp).Row”&Chr(13)和_
“.Range(“$A$6:$BQ$”&lastrow)。自动筛选字段:=30,标准1:=”&Chr(34)和“>=”&Chr(34)和“&”&Chr(34)和日期值(表(“生产能力”)。单元格(i,“A”)。值和Chr(34)和“,运算符:=xlAnd,标准2:=”&Chr(34)和“归功于@DisplayName!非常感谢您帮助我简化我的解决方案并停止设计它。我的UserForm的新Sub如下所示:
Dim mColButtons As New Collection
Private Sub CommandButton5_Click()
Dim lastrow As Long, i As Integer, numButtons As Integer
Dim btnEvent As Class1
Dim ctl As MSForms.Control
numButtons = 1
With Sheets("Production Capacity")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A4:AD" & lastrow).Interior.Color = RGB(255, 255, 255)
For i = 4 To lastrow
If i Mod 4 = 0 Then
If .Cells(i, "D").Value > .Cells(2, "G").Value Then
.Cells(i, "G").Interior.Color = RGB(255, 0, 0)
Set ctl = Me.Controls.Add("Forms.CommandButton.1")
With ctl
.Width = 200
Select Case (numButtons Mod 3)
Case 0
.Left = 475
Case 1
.Left = 25
Case 2
.Left = 250
End Select
.Visible = True
.Height = 20
.Top = 60 + (Int((numButtons - 1) / 3) * 40)
.Caption = Sheets("Production Capacity").Cells(i, "A").Value & " - " & Sheets("Production Capacity").Cells(i, "B").Value & " DeptName"
.Font.Size = 10
.Font.Bold = True
.Name = "button" & numButtons
End With
Set btnEvent = New Class1
Set btnEvent.btn = ctl
Set btnEvent.frm = Me
mColButtons.Add btnEvent
numButtons = numButtons + 1
End If
我的类模块现在看起来像这样,它将所有逻辑简化为一个简洁的Select语句。再次感谢你
Public WithEvents btn As MSForms.CommandButton
Public frm As UserForm
Private Sub btn_click()
Dim startDate As String, endDate As String, department As String, lastrow As Long
startDate = Split(btn.Caption, " ")(0)
endDate = Split(btn.Caption, " ")(2)
department = Split(btn.Caption, " ")(3)
With Sheets(Sheets("Production Capacity").Cells(1, "A").Value)
lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
Select Case department
Case "Veneering"
.ShowAllData
.Range("A$6:$BQ$" & lastrow).AutoFilter field:=21, Criteria1:=">=" & DateValue(startDate), Operator:=xlAnd, Criteria2:="<=" & DateValue(endDate), Operator:=xlAnd
Case "MillMachining"
.ShowAllData
.Range("A$6:$BQ$" & lastrow).AutoFilter field:=30, Criteria1:=">=" & DateValue(startDate), Operator:=xlAnd, Criteria2:="<=" & DateValue(endDate), Operator:=xlAnd
Case "BoxLine"
.ShowAllData
.Range("A$6:$BQ$" & lastrow).AutoFilter field:=39, Criteria1:=">=" & DateValue(startDate), Operator:=xlAnd, Criteria2:="<=" & DateValue(endDate), Operator:=xlAnd
Case "Custom"
.ShowAllData
.Range("A$6:$BQ$" & lastrow).AutoFilter field:=48, Criteria1:=">=" & DateValue(startDate), Operator:=xlAnd, Criteria2:="<=" & DateValue(endDate), Operator:=xlAnd
Case "Finishing"
.ShowAllData
.Range("A$6:$BQ$" & lastrow).AutoFilter field:=57, Criteria1:=">=" & DateValue(startDate), Operator:=xlAnd, Criteria2:="<=" & DateValue(endDate), Operator:=xlAnd
End Select
End With
End Sub
Public with events btn As MSForms.CommandButton
作为用户表单的公共frm
专用子btn_单击()
Dim startDate为字符串,endDate为字符串,department为字符串,lastrow为长
开始日期=拆分(btn.Caption,“”)(0)
结束日期=拆分(btn.Caption,“”)(2)
部门=拆分(btn.Caption,“”)(3)
带板材(板材(“生产能力”)。单元(1,“A”)。值)
lastrow=.Cells(Rows.Count,“B”).End(xlUp).Row
选择病例科室
案例“贴面”
.ShowAllData
.Range(“A$6:$BQ$”&lastrow)。自动筛选字段:=21,准则1:=“>=”&DateValue(startDate),运算符:=xlAnd,准则2:=”&DateValue(startDate),运算符:=xlAnd,准则2:=”&DateValue(startDate),运算符:=xlAnd,准则2:=”&DateValue(StartDateDate),运算符:=AND,准则2:=”&DateValue(StartDateDateDate),运算符:=AND,准则2:=“你有没有看过这两个答案中的一个看起来很有趣。我已经尝试了这两个建议,但都没有成功。在类模块中创建事件是我最新的尝试,但这限制了我让每个按钮做相同的事情,或者需要将每个按钮定义为单独的类。将声明更改为VBIDE类的组件也会在同一位置产生相同的错误。“但这限制了我让每个按钮做相同的事情,”:如果我没有错,代码生成的所有事件处理程序的代码中唯一的区别在于嵌套在.Cells(I,“A”)中的I
.Value
。在这种情况下,您可以让类按钮从按钮中重新创建i
值caption@DisplayName非常感谢。我在下面发布了我的最终解决方案,非常感谢您的帮助。