vba 如何在Excel VBA中捕获多页控件的页面退出事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15552360/
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 to catch page exit event of multipage control in Excel VBA
提问by Excel Developers
I have a multipage control on a userform. When the user navigates to a new page, I want the data on the current page to be saved to a database. The user can navigate between pages in various ways and I don't want to write a procedure for all of them. Is there an event I can use to reference the active page before the new page is selected?
我在用户窗体上有一个多页控件。当用户导航到新页面时,我希望将当前页面上的数据保存到数据库中。用户可以通过各种方式在页面之间导航,我不想为所有这些页面编写一个过程。在选择新页面之前,是否有可以用来引用活动页面的事件?
I have looked at the Change event of the multipage control but if you reference multipage.selecteditem in there it refers to the new page. What I need is a BeforeChange event but there isn't one.
我已经查看了多页控件的 Change 事件,但如果您在其中引用 multipage.selecteditem,则它指的是新页面。我需要的是 BeforeChange 事件,但没有。
I have also looked at the multipage.exit event, but that triggers when the entire multipage control is exited, not just a page.
我还查看了 multipage.exit 事件,但它会在退出整个多页控件时触发,而不仅仅是一个页面。
Any ideas?
有任何想法吗?
回答by David Zemens
Use the MultiPage1_Change
event.
使用MultiPage1_Change
事件。
If you have assigned a name to your Multipage, then you would change the subroutine from MultiPage1_Change
(which is the default) to YourMultiPageName_Change
.
如果您为 Multipage 指定了一个名称,那么您可以将子例程从MultiPage1_Change
(这是默认值)更改为YourMultiPageName_Change
.
for example I have one that is called "MultiPageBannerFilter". I execute some code using this subroutine to trap the _Change event of this object.
例如我有一个叫做“MultiPageBannerFilter”的。我使用这个子程序执行一些代码来捕获这个对象的 _Change 事件。
Private Sub MultiPageBannerFilter_Change()
MsgBox "You have changed pages!", vbInformation
End Sub
Just put your code to save the info to database instead of the messagebox, and make sure the event subroutine is properly named and this should work.
只需将您的代码用于将信息保存到数据库而不是消息框,并确保事件子例程正确命名并且这应该可以工作。
REVISION
修订
Public previousPage As String
Sub UserForm_Activate()
previousPage = MultiPage1.SelectedItem.Name
End Sub
Private Sub MultiPage1_Change()
Dim currentPage As String
currentPage = MultiPage1.SelectedItem.Name
If Not currentPage = previousPage Then
previousPage = currentPage
'
'
MsgBox "Your Code Goes Here!", vbInformation
'
'
End If
End Sub
回答by Russ Mygrant
I had a similar challenge and came up with the below code. You have to keep track of which Page you're on and use the Click event. So, set the Multipage to a Page in the Initialize sub and set intPrevPage to the corresponding number.
我遇到了类似的挑战,并提出了以下代码。您必须跟踪您所在的页面并使用 Click 事件。因此,在 Initialize 子项中将 Multipage 设置为 Page 并将 intPrevPage 设置为相应的数字。
Private Sub MultiPage1_Click(ByVal Index As Long)
Select Case True
' If the click doesn't change the Page, then do nothing
Case intPrevPage = MultiPage1.Value
' ValidForm is a function that validates data on previous
' Page if click changed the Page
Case ValidForm
' If TRUE then capture current Page
intPrevPage = MultiPage1.Value
Call FormatPage
' If FALSE then return to previous Page
Case Else
MultiPage1.Value = intPrevPage
End Select
End Sub
回答by osprey
MultiPage1_Change event will only be fired if there is a page. Therefore, it's not necessary to detect the change and use the public variable "previousPage"
MultiPage1_Change 事件只会在有页面时触发。因此,没有必要检测更改并使用公共变量“previousPage”
Private Sub MultiPage1_Change() Dim currentPage As String currentPage = MultiPage1.SelectedItem.Name MsgBox currentPage & " Selected!", vbInformation End Sub
Private Sub MultiPage1_Change() Dim currentPage As String currentPage = MultiPage1.SelectedItem.Name MsgBox currentPage & "Selected!", vbInformation End Sub