如何使用VB.NET将VBA代码放在excel表的命令按钮中?
这是我在sheet1中创建命令按钮并在模块中添加代码的代码:
Here is my code in creating a command button in sheet1 and adding code in module:
Dim xlMod As Microsoft.Vbe.Interop.VBComponent
Dim objBtn As Object
Dim celLeft As Integer
Dim celTop As Integer
Dim celWidth As Integer
Dim celHeight As Integer
celLeft = xlWorkSheet1.Range("d5").Left
celTop = xlWorkSheet1.Range("d5").Top
celWidth = xlWorkSheet1.Range("d5").Width
celHeight = xlWorkSheet1.Range("d5").Height
objBtn = xlWorkSheet1.OLEObjects.Add(ClassType:="Forms.Commandbutton.1", link:=False, _
displayasicon:=False, Left:=celLeft, Top:=celTop, Width:=celWidth, Height:=celHeight)
xlWorkSheet1.commandbutton1.caption = "Generate Sheet 2"
xlWorkSheet1.commandbutton1.Height = 33
xlWorkSheet1.commandbutton1.Width = 125
xlWorkSheet1.commandbutton1.font.size = "12"
xlMod = xlWorkBook.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule)
Dim codeString As New StringBuilder
codeString.AppendLine("Public Sub SaySomething()")
codeString.AppendLine("MsgBox ""Hello""")
codeString.AppendLine("End Sub")
xlMod.CodeModule.AddFromString(codeString.ToString)
我想要的是将一个vba代码放在sheet1 commandbutton1_click事件中,而不是放在其他模块中。
请帮助我。
我尝试过:
All I want is to put a vba code inside the sheet1 commandbutton1_click event and not in other Module.
Please help me.
What I have tried:
xlMod = xlWorkBook.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule)
Dim codeString As New StringBuilder
codeString.AppendLine("Public Sub SaySomething()")
codeString.AppendLine("MsgBox ""Hello""")
codeString.AppendLine("End Sub")
xlMod.CodeModule.AddFromString(codeString.ToString)
如何使用VB.NET将VBA代码放在excel表的命令按钮中?
How to put a VBA code inside command button of excel sheet using VB.NET?
< vba co de =inside =command =button=does =not =存在。=a =button =can =only =call = subroutine =by =its =name。
< pre =lang =vb>
xlWorkSheet1.commandbutton1.caption =生成工作表2
xlWorkSheet1.commandbutton1.Height = 33
xlWorkSheet1.commandbutton1.Width = 125
xlWorkSheet1.commandbutton1.font.size = 12
您已经设置了 commandbutton1
的一些属性,将一些代码链接到按钮事件只是设置另一个属性,为什么这么难?
"<vba code="" inside="" command="" button"="" does="" not="" exist.="" a="" button="" can="" only="" call="" subroutine="" by="" its="" name.
<pre="" lang="vb">
xlWorkSheet1.commandbutton1.caption = "Generate Sheet 2"
xlWorkSheet1.commandbutton1.Height = 33
xlWorkSheet1.commandbutton1.Width = 125
xlWorkSheet1.commandbutton1.font.size = "12"
You are already setting some properties of commandbutton1
, linking some code to a button event is just setting another property, why is it so difficult?