Events 如何将事件添加到使用VBA在Excel中运行时创建的控件
我想在Excel中使用VBA在运行时添加控件和关联事件,但我不知道如何添加事件 我尝试了下面的代码,在我的userform中正确创建了按钮,但是应该显示hello消息的关联click事件不起作用 欢迎提供任何建议/更正Events 如何将事件添加到使用VBA在Excel中运行时创建的控件,events,vba,controls,Events,Vba,Controls,我想在Excel中使用VBA在运行时添加控件和关联事件,但我不知道如何添加事件 我尝试了下面的代码,在我的userform中正确创建了按钮,但是应该显示hello消息的关联click事件不起作用 欢迎提供任何建议/更正 Dim Butn As CommandButton Set Butn = UserForm1.Controls.Add("Forms.CommandButton.1") With Butn .Name = "CommandButton1" .Caption = "
Dim Butn As CommandButton
Set Butn = UserForm1.Controls.Add("Forms.CommandButton.1")
With Butn
.Name = "CommandButton1"
.Caption = "Click me to get the Hello Message"
.Width = 100
.Top = 10
End With
With ThisWorkbook.VBProject.VBComponents("UserForm1.CommandButton1").CodeModule
Line = .CountOfLines
.InsertLines Line + 1, "Sub CommandButton1_Click()"
.InsertLines Line + 2, "MsgBox ""Hello!"""
.InsertLines Line + 3, "End Sub"
End With
UserForm1.Show
我认为代码需要添加到Userform,而不是按钮本身 大概是
With UserForm1.CodeModule
'Insert code here
End With
使用此工作簿取代您的,
尝试以下操作:
Sub AddButtonAndShow()
Dim Butn As CommandButton
Dim Line As Long
Dim objForm As Object
Set objForm = ThisWorkbook.VBProject.VBComponents("UserForm1")
Set Butn = objForm.Designer.Controls.Add("Forms.CommandButton.1")
With Butn
.Name = "CommandButton1"
.Caption = "Click me to get the Hello Message"
.Width = 100
.Top = 10
End With
With objForm.CodeModule
Line = .CountOfLines
.InsertLines Line + 1, "Sub CommandButton1_Click()"
.InsertLines Line + 2, "MsgBox ""Hello!"""
.InsertLines Line + 3, "End Sub"
End With
VBA.UserForms.Add(objForm.Name).Show
End Sub
这将永久修改UserForm1(假设您保存工作簿)。如果您想要一个临时userform,那么添加一个新的userform,而不是将其设置为UserForm1。完成后,您可以删除表单
关于VBE的编码有一些很好的信息。在运行时添加按钮然后添加事件的代码确实很简单,因为很难找到。我可以这么说,因为我花了更多的时间在这个困惑上,比我曾经编程过的任何东西都更恼火 创建Userform并输入以下代码:
Option Explicit
Dim ButArray() As New Class2
Private Sub UserForm_Initialize()
Dim ctlbut As MSForms.CommandButton
Dim butTop As Long, i As Long
'~~> Decide on the .Top for the 1st TextBox
butTop = 30
For i = 1 To 10
Set ctlbut = Me.Controls.Add("Forms.CommandButton.1", "butTest" & i)
'~~> Define the TextBox .Top and the .Left property here
ctlbut.Top = butTop: ctlbut.Left = 50
ctlbut.Caption = Cells(i, 7).Value
'~~> Increment the .Top for the next TextBox
butTop = butTop + 20
ReDim Preserve ButArray(1 To i)
Set ButArray(i).butEvents = ctlbut
Next
End Sub
现在你需要在项目代码中添加一个类模块。请记住它的类模块而不是模块。然后输入以下简单代码(在我的例子中,类名是Class2)-
就这样。现在运行它DaveShaw,谢谢这个代码人 我将其用于togglebutton数组(将名为trainer.jpg的“缩略图大小”图片放在excel文件的同一文件夹中,该文件夹用于包含图片的togglebutton)。在“click”事件中,调用程序也可用(通过对象名称作为字符串) 形式如下:
Dim CreateTrainerToggleButtonArray() As New ToggleButtonClass
Private Sub CreateTrainerToggleButton(top As Integer, id As Integer)
Dim pathToPicture As String
pathToPicture = ThisWorkbook.Path & "\trainer.jpg"
Dim idString As String
idString = "TrainerToggleButton" & id
Dim cCont As MSForms.ToggleButton
Set cCont = Me.Controls.Add _
("Forms.ToggleButton.1")
With cCont
.Name = idString
.Width = 20
.Height = 20
.Left = 6
.top = top
.picture = LoadPicture(pathToPicture)
End With
ReDim Preserve CreateTrainerToggleButtonArray(1 To id)
Set CreateTrainerToggleButtonArray(id).ToggleButtonEvents = cCont
CreateTrainerToggleButtonArray(id).ObjectName = idString
End Sub
还有一个“ToggleButtonClass”类
这是我在不使用类的情况下添加commandbutton和代码的解决方案 它添加了一个引用以允许访问vbide 添加按钮 然后编写一个函数来处理工作表中的单击事件
子添加按钮()
调用addref
设置rng=DestSh.范围(“B”和x+3)
'设置btn=DestSh.Buttons.Add(rng.Left,rng.Top,rng.Width,rng.Height)
设置myButton=ActiveSheet.OLEObjects.Add(类类型:=“Forms.CommandButton.1”,左:=rng.Left,顶:=rng.Top,高:=rng.Height*3,宽:=rng.Width*3)
多芬特
用我的按钮
'.Placement=XlPlacement.xlFreeFloating
.Object.Caption=“导出”
.Name=“BtnExport”
.Object.PicturePosition=1
.Object.Font.Size=14
以
停止
myButton.Object.Picture=LoadPicture(“F:\Finalized reports\Templates\Macros\evolution48.bmp”)
调用CreateButtonEvent
端接头
子addref()
出错时继续下一步
Application.VBE.ActiveVBProject.References.AddFromFile“C:\Program Files(x86)\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB”
Application.VBE.ActiveVBProject.References.AddFromFile“C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB”
端接头
私有子CreateButtonEvent()
关于错误转到错误陷阱
Dim oXl作为应用程序:设置oXl=应用程序
oXl.EnableEvents=False
oXl.DisplayAlerts=False
oXl.ScreenUpdate=False
oXl.VBE.MainWindow.Visible=False
将oWs设置为工作表
将oVBproj作为VBIDE.VBProject进行调整
将oVBcomp作为VBIDE.VBComponent进行调整
将oVBmod设置为VBIDE.CodeModule'
单瓣淡黄色
常量引号为字符串“”
设置oWs=工作表(“应急”)
设置oVBproj=thiswoolk.VBProject
设置oVBcomp=oVBproj.VBComponents(oWs.CodeName)
设置oVBmod=oVBcomp.CodeModule
带oVBmod
lLine=.CreateEventProc(“单击”、“BtnExport”)+1
.InsertLines“调用CSVFile”
以
oXl.EnableEvents=True
oXl.DisplayAlerts=True
出口接头
错误陷阱:
端接头
一种简单的方法:
1-插入类模块并编写以下代码:
Public WithEvents ChkEvents As MSForms.CommandButton
Private Sub ChkEvents_click()
MsgBox ("Click Event")
End Sub
Dim Chk As New Clase1
Private Sub UserForm_Initialize()
Dim NewCheck As MSForms.CommandButton
Set NewCheck = Me.Controls.Add("Forms.CommandButton.1")
NewCheck.Caption = "Prueba"
Set Chk.ChkEvents = NewCheck
End Sub
2-插入用户表单并编写以下代码:
Public WithEvents ChkEvents As MSForms.CommandButton
Private Sub ChkEvents_click()
MsgBox ("Click Event")
End Sub
Dim Chk As New Clase1
Private Sub UserForm_Initialize()
Dim NewCheck As MSForms.CommandButton
Set NewCheck = Me.Controls.Add("Forms.CommandButton.1")
NewCheck.Caption = "Prueba"
Set Chk.ChkEvents = NewCheck
End Sub
现在显示表单并单击按钮请参阅,以获取在VBA.com中使用运行时挂钩事件的快速介绍。谢谢,我有完全相同的疑问,回答得很好!轻松修改其他类型表单的示例!我似乎无法使
Me.Controls.Add(…)
在Access 2010 Professional中使用.mdb文件。我可以添加Microsoft Forms 2.0对象库
,但是在Acc2010中没有Microsoft Access类对象
窗体控件(或控件或窗体)对象的add
方法,该对象与Me
对象关联。我相信我必须做一个CreateControl
,但仍在为细节而挣扎。我想补充一点,如果您使用ActiveX控件并通过currCtrl=sheet)“mysheet”调用它们。OLEObjects.Item(I)
您必须通过将它们添加到或中currCtrl.Object
,如上所述。
Dim Chk As New Clase1
Private Sub UserForm_Initialize()
Dim NewCheck As MSForms.CommandButton
Set NewCheck = Me.Controls.Add("Forms.CommandButton.1")
NewCheck.Caption = "Prueba"
Set Chk.ChkEvents = NewCheck
End Sub