I previously asked a question and received a response from T.M. that answered it perfectly!
但是,当我以辅助形式输入代码时,会收到与主形式中最初收到的错误相同的错误。
下面是放置在表单和类模块中的代码以及一些屏幕截图,它们可以更好地说明第二种表单,因为我不确定我是否解释得很清楚...
非常感谢大家!
采用以下格式的代码:
Dim cBar As clsBar
Private Sub UserForm_Initialize()
On Error GoTo Whoa
Application.EnableEvents = False
Set cBar = New clsBar
cBar.Initialize Me
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
类模块中的代码
'Popup objects
Private cmdBar As CommandBar
Private WithEvents cmdCopyButton As CommandBarButton
Private WithEvents cmdPasteButton As CommandBarButton
'Useform to use
Private fmUserform As Object
'Control array of textbox
Private colControls As Collection
'Textbox Control
Private WithEvents tbControl As MSForms.TextBox
'Adds all the textbox in the userform to use the popup bar
Sub Initialize(ByVal UF As Object)
Dim Ctl As MSForms.Control
Dim cBar As clsBar
For Each Ctl In UF.Controls
If TypeName(Ctl) = "TextBox" Then
'Check if we have initialized the control array
If colControls Is Nothing Then
Set colControls = New Collection
Set fmUserform = UF
'Create the popup
CreateBar
End If
'Create a new instance of this class for each textbox
Set cBar = New clsBar
cBar.AssignControl Ctl, cmdBar
'Add it to the control array
colControls.Add cBar
End If
Next Ctl
End Sub
Private Sub Class_Terminate()
'Delete the commandbar when the class is destroyed
On Error Resume Next
cmdBar.Delete
End Sub
'Click event of the copy button
Private Sub cmdCopyButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
Dim sACN As String
sACN = ActiveControlName(fmUserform) ' find control's name
' Debug.Print sACN & ".Copy"
fmUserform.Controls(sACN).copy ' << instead of fmUserform.ActiveControl.Copy
CancelDefault = True
End Sub
'Click event of the paste button
Private Sub cmdPasteButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
Dim sACN As String
sACN = ActiveControlName(fmUserform)
' Debug.Print sACN & ".Paste"
fmUserform.Controls(sACN).Paste ' << instead of fmUserform.ActiveControl.Paste
CancelDefault = True
End Sub
'Right click event of each textbox
Private Sub tbControl_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 And Shift = 0 Then
'Display the popup
cmdBar.ShowPopup
End If
End Sub
Private Sub CreateBar()
Set cmdBar = Application.CommandBars.Add(, msoBarPopup, False, True)
'We’ll use the builtin Copy and Paste controls
Set cmdCopyButton = cmdBar.Controls.Add(ID:=19)
Set cmdPasteButton = cmdBar.Controls.Add(ID:=22)
End Sub
'Assigns the Textbox and the CommandBar to this instance of the class
Sub AssignControl(TB As MSForms.TextBox, Bar As CommandBar)
Set tbControl = TB
Set cmdBar = Bar
End Sub
Function ActiveControlName(form As UserForm) As String
'cf Site: https://stackoverflow.com/questions/47745663/get-activecontrol-inside-multipage
'Purpose: get ActiveControl
Dim MyMultiPage As MSForms.MultiPage, myPage As MSForms.Page
If form.ActiveControl Is Nothing Then
' do nothing
ElseIf TypeName(form.ActiveControl) = "MultiPage" Then
Set MyMultiPage = form.ActiveControl
Set myPage = MyMultiPage.SelectedItem
ActiveControlName = myPage.ActiveControl.Name
Else
ActiveControlName = form.ActiveControl.Name
End If
End Function
在第二个表单中单击“复制”或“粘贴”时,出现错误:
运行时错误'438':
对象不支持此属性或方法。在线:
fmUserform.Controls(sACN).Paste
答案 0 :(得分:0)
对第二个UserForm调用的必要修改
似乎出现了直接在第一个表单中显示第二个表单的问题,因为单击事件也从被单击的“ CAT”控件中获取了返回值。
这对我有用:
ShowYODA
显示第二个用户表单,即调用位于单独模块中的过程,例如在单独的模块中调用第二种表单的示例
Sub ShowYODA
With New YODA ' temporary new UF instance
.Show vbModeless
End With
End Sub
相关点击事件
'Click event of the copy button
Private Sub cmdCopyButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
Dim sACN As String
sACN = ActiveControlName(fmUserform) ' find control's name
If Right$(sACN, 1) = " " Then ' marker (=ending space) for textboxes only!
Debug.Print Trim(sACN) & ".Copy"
fmUserform.Controls(Trim(sACN)).Copy ' << instead of fmUserform.ActiveControl.Copy /438 unterstü.d.Meth nicht!
CancelDefault = True
End If
End Sub
'Click event of the paste button
Private Sub cmdPasteButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
Dim sACN As String
sACN = ActiveControlName(fmUserform) ' find control's name
If Right$(sACN, 1) = " " Then ' marker (=ending space) for textboxes only!
Debug.Print Trim(sACN) & ".Copy"
fmUserform.Controls(Trim(sACN)).Paste ' << instead of fmUserform.ActiveControl.Copy
CancelDefault = True
End If
End Sub
修改后的辅助功能ActiveControlName()
Function ActiveControlName(form As MSForms.UserForm) As String
'cf Site: https://stackoverflow.com/questions/47745663/get-activecontrol-inside-multipage
'Purpose: get ActiveControl name string and mark text boxes by an ending space
Dim MyMultiPage As MSForms.MultiPage, myPage As MSForms.Page
If form.ActiveControl Is Nothing Then
' do nothing
ElseIf TypeName(form.ActiveControl) = "MultiPage" Then
Set MyMultiPage = form.ActiveControl
Set myPage = MyMultiPage.SelectedItem
ActiveControlName = myPage.ActiveControl.Name
If TypeName(form.Controls(ActiveControlName)) = "TextBox" Then ActiveControlName = ActiveControlName & " "
Else
ActiveControlName = form.ActiveControl.Name
If TypeName(form.Controls(ActiveControlName)) = "TextBox" Then ActiveControlName = ActiveControlName & " "
End If
End Function