vba 如何在excel VBA中控制多页内的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15192801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you control a button inside a multipage in excel VBA
提问by Jovanni G
I have a multipage, I was successfully able to copy elements of the first page which is my reference page to new pages which is created dynamically.
我有一个多页,我成功地将第一页的元素复制到动态创建的新页面,这是我的参考页。
My question is, how do I set a commandbutton's actions inside a page in a multipage control? My goal is to click on the button from any page then pops up another form.
我的问题是,如何在多页控件的页面内设置命令按钮的操作?我的目标是单击任何页面上的按钮,然后弹出另一个表单。
How do I do this? It's pretty hard to adjust from Android to VB. I really appreciate any help from you guys.
我该怎么做呢?从 Android 调整到 VB 非常困难。我真的很感谢你们的任何帮助。
This is my code in cloning pages.
这是我在克隆页面中的代码。
i = 0
MultiPage1.Pages.Add
MultiPage1.Pages(i).Controls.Copy
i = i + 1
MultiPage1.Pages(i).Paste
For Each ctl In Me.MultiPage1.Pages(i).Controls
If TypeOf ctl Is MSForms.Label Then
'~~~ code omitted
Select Case ctl.Tag
Case "startTime"
ctl.Caption = "4:00pm"
End Select
End If
Next
this is how it's going to look like.
这就是它的样子。
the button will concatenate all strings inside the page. the concatenated string will be shown on another userform.
该按钮将连接页面内的所有字符串。连接的字符串将显示在另一个用户表单上。
采纳答案by Joseph
You probably would be better off creating a button in the ribbon so that it is available on all pages:
您可能最好在功能区中创建一个按钮,以便它在所有页面上可用:
http://chandoo.org/wp/2012/02/27/how-to-add-your-own-macros-to-excel-ribbon/
http://chandoo.org/wp/2012/02/27/how-to-add-your-own-macros-to-excel-ribbon/
EDIT:
编辑:
My bad, I thought you meant a worksheet instead of a VBA MultiPage in a userform.
我的错,我以为你的意思是工作表而不是用户表单中的 VBA MultiPage。
Check this out. I was able to make this work for me:
看一下这个。我能够为我完成这项工作:
Assign code to a button created dynamically
Class1:
类1:
Option Explicit
Public WithEvents CmdEvents As MSForms.CommandButton
Private Sub CmdEvents_Click()
MsgBox "yo"
End Sub
Userform with MultiPage object:
带有 MultiPage 对象的用户窗体:
Option Explicit
Dim cmdArray() As New Class1
Private Sub CommandButton1_Click()
Dim newControl As Control
Set newControl = Me.MultiPage1.Pages(0).Controls.Add("Forms.CommandButton.1", "NewCommand", True)
newControl.Object.Caption = "hello"
newControl.Left = 50
newControl.Top = 50
ReDim Preserve cmdArray(1 To 1)
Set cmdArray(1).CmdEvents = newControl
Set newControl = Nothing
End Sub
回答by Peter Albert
You can do this with a custom class. The class basically has one member Public WithEvents b as CommandButton
.
您可以使用自定义类来执行此操作。班级基本上只有一名成员Public WithEvents b as CommandButton
。
The trick is the WithEvents
keyword. You can now insert some code to generically handle the click of a button that is assigned to this class:
诀窍是WithEvents
关键字。您现在可以插入一些代码来一般处理分配给此类的按钮的单击:
Private Sub b_Click()
MsgBox "You clicked " & b.Name 'Modify this event so that different code is executed base on the page/name/etc.
End Sub
In order to make this work, you need to assign the button you create in your code to an object of this new class:
为了完成这项工作,您需要将您在代码中创建的按钮分配给这个新类的对象:
Private objButtonHandler as New MyClass 'this should be scope a UserForm wide
Sub YourSub
Dim objYourButton as CommandButton
Set objYourButton = ... `your code here
Set objButtonHandler.b = objYourButton
End Sub