我正在为我们的建筑工地访问制定启用宏的Excel清单。该文件是一个相当简单的清单,可以自动生成网格并添加ActiveX预编程按钮,以便用户可以通过用户窗体轻松输入信息。
我使用一个子项来动态创建ActiveX按钮(它们是OLEObjects)。该子程序的工作原理与预期的相同,但是我正努力寻找信息,但有一个小麻烦。每次创建按钮时,Excel的“ Microsoft Visual Basic应用程序编辑器”页面都会打开,以“向我显示”已添加的代码。它很烦人,使文件看起来不专业。
代码添加到:ThisWorkbook.VBProject.VBComponents.Item(“ Sheet1”)。CodeModule
VBA编辑器打开,向我显示“ Sheet1”代码。 添加的代码是使用新创建的Commandbutton的事件(单击)
Sub包含在模块中。 我可以添加任何代码行来阻止打开VBA页面吗? 这很奇怪,因为我的错误处理程序是默认的(发生错误时出错),并且不显示任何错误,也不会中断。
感谢您的时间!
Sub Create_button(ByVal Target_Row As String, ByVal Target_Col As String)
Dim Target As String
Target = Target_Col & Target_Row
Dim oole As OLEObject
Dim CodeMod As Object
Dim Code As String
Dim Line As Integer
Set oole = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, _
DisplayAsIcon:=False, Left:=(Range(Target).Left + 1), Top:=(Range(Target).Top + 1), Width:=46, Height _
:=43)
With oole.Object
.Caption = "MaJ"
End With
Set CodeMod = ThisWorkbook.VBProject.VBComponents.Item("Sheet1").CodeModule
With CodeMod
Line = .CreateEventProc("Click", oole.Name) + (Button_number + 1)
Code = "Call MaJ_avancement(" & oole.Name & "_Row, " & oole.Name & "_Column)"
.InsertLines Line, Code
Code = oole.Name & "_Column = " & oole.Name & ".TopLeftCell.Column"
.InsertLines Line, Code
Code = oole.Name & "_Row = " & oole.Name & ".TopLeftCell.Row"
.InsertLines Line, Code
Code = "Dim CommandButton1_Row, CommandButton1_Column"
.InsertLines Line, Code
End With
End Sub
在“ Sheet1”对象中添加的代码如下:
Private Sub CommandButton1_Click()
Dim CommandButton1_Row, CommandButton1_Column
CommandButton1_Row = CommandButton1.TopLeftCell.Row
CommandButton1_Column = CommandButton1.TopLeftCell.Column
Call MaJ_avancement(CommandButton1_Row, CommandButton1_Column)
End Sub