这是类模块的代码(代码模块上的错误所在):
Private Sub CommandButton_Click()
Dim VBAEditor As VBIDE.VBE
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim mdl As Object
Dim mdl_exits As Boolean
Dim mdl_name As String
Dim macro_name As String
Dim macro_exists As Boolean
mdl_name = "SaveButtons"
For Each mdl In ThisWorkbook.VBProject.VBComponents
If mdl.Name = mdl_name And mdl.Type = 1 Then
Set prrf_Module = mdl
mdl_exists = True
Exit For
End If
Next
If mdl_exists Then GoTo it_exists
Set prrf_Module = ThisWorkbook.VBProject.VBComponents.Add(1)
prrf_Module.Name = mdl_name
it_exists:
macro_exists = False
macro_name = SaveButton.Value
new_name:
If macro_exists = True Then
If macro_name = SaveButton.Value Then
macro_name = SaveButton.Value & "1"
Else
macro_name = Left(macro_name, Len(macro_name) - 1) & CInt(Mid(macro_name, Len(macro_name) - 1)) + 1
End If
End If
macro_exists = False
zy = "Userform1.show"
strMacro = "Sub " & "CommandButton1" & vbCr
strMacro = strMacro & " " & zy & vbCr
strMacro = strMacro & "End Sub" & vbCr
Debug.Print "strMacro is " & vbCr & strMacro
'Set prrf_Module = ThisWorkbook.VBProject.VBComponents.Add(1)
Dim d, e, f, y
For y = 1 To 2
With btn_Gen.CodeModule
d = .CountOfLines
.insertlines 1, "Sub CommandButton" & y & "_Click()"
For e = LBound(t) To UBound(t)
countlines = countlines + 1
upper = UBound(t)
xy = countlines + 1
.insertlines xy, " " & t(e)
Next e
.insertlines xy + 1, "End Sub"
End With
Next y
End Sub
这是我的模块代码(一直运行到我单击commandbutton1为止)
Sub showuserform1()
Dim x, y
Dim z() As String
Set btn_nm = New Scripting.Dictionary
Set lbl_tx = New Scripting.Dictionary
Set code = New Scripting.Dictionary
repeatx:
x = UCase(InputBox("(H)orizontal or (V)ertical?", "Orientation", "H"))
If x = "H" Then
orientation = "Horizontal"
ElseIf x = "V" Then
orientation = "Vertical"
Else
MsgBox ("Input either 'H' or 'V'")
GoTo repeatx
End If
repeatcount:
count = InputBox("How many buttons do you want? ", "Button Count", "1")
If count < 1 Then
MsgBox ("Input Quantity of Buttons, enter at least 1")
GoTo repeatcount:
End If
For y = 1 To count
btn_nm(y) = InputBox("What do you want CommandButton" & y + 1 & " to say?", "CommandButton name", "CommandButton" & y)
lbl_tx(y) = InputBox("What do you want Label" & y + 1 & " to say?", "Label text", "Label" & y)
btn_Gen_uf2.Label1.Caption = "Enter Code in Window - max 8k characters"
btn_Gen_uf2.TextBox1.Value = ""
repeatshow:
btn_Gen_uf2.TextBox1.SetFocus
btn_Gen_uf2.Show
If btn_Gen_uf2.TextBox1.Value = "" Then
MsgBox "Enter Code in Window before selecting OK"
GoTo repeatshow:
End If
t = Split(btn_Gen_uf2.TextBox1.Text, vbCrLf)
ReDim z(0 To UBound(t), 1 To count)
For w = LBound(t) To UBound(t)
MsgBox t(w)
'Debug.Print "z(" & w & "," & y & ") = " & t(w)
z(w, y) = t(w)
Next w
Next y
'do this last
btn_Gen.Show
End Sub
与excelforum交叉张贴
我添加了声明(请参见上文),现在在此行出现错误:Dim VBAEditor as VBIDE.VBE 错误是未定义的用户定义类型。我缺少参考吗?
感谢您的所有帮助。
答案 0 :(得分:0)
因此在rory的帮助下,我进行了一次更改,并且大多数代码都在工作。
With Prrf_Module.CodeModule
代替
With btn_Gen.Codemodule
再次感谢罗伊!