VBA - 根据组合框选择在框架中隐藏页面(选项卡)
VBA - Hide pages (tabs) in a frame based on Combobox selection
我已经编写了 VBA 代码来根据组合框选择显示隐藏选项卡。组合框中有七个选项,每个选项对应框架中的七个隐藏选项卡。
Private Sub CBO_EntryType_Change()
Dim iPage As Integer
If Me.CBO_EntryType.Value = "Abstracts" Then
iPage = 1
ElseIf CBO_EntryType.Value = "Awards" Then
iPage = 2
ElseIf CBO_EntryType.Value = "Career Fairs" Then
iPage = 3
ElseIf CBO_EntryType.Value = "Editorials" Then
iPage = 4
ElseIf CBO_EntryType.Value = "Rankings" Then
iPage = 5
ElseIf CBO_EntryType.Value = "Tradeshows" Then
iPage = 6
ElseIf CBO_EntryType.Value = "Social Media" Then
iPage = 7
End If
Me.MultiPage1.Pages(iPage).Visible = True
End Sub
我似乎遇到的问题是,如何确保隐藏其他选项卡?由于人们只能单击组合框中的一个选项,但他们可能会误单击一个,然后单击正确的选项。根据组合框中的选定项目,只有一个选项卡应该可见。其他六个应该隐藏。
我想在 sub 的末尾使用 For-Each-Next 循环禁用任何与 iPage 变量不匹配的选项卡,但我很难弄清楚如何处理框架和页面For Each Next 循环。变量声明是什么?
未经测试,因此可能需要稍作调整...
Private Sub CBO_EntryType_Change()
Dim iPage, arrPages, x As Long
arrPages = Array("Abstracts", "Awards", "Career Fairs", "Editorials", _
"Rankings", "Tradeshows", "Social Media")
'find the index in the array...
iPage = Application.Match(Me.CBO_EntryType.Value, arrPages, 0)
'if got a match then loop over the pages and show/hide
If Not IsError(iPage) Then
For x = 0 To Me.MultiPage1.Pages.Count-1
Me.MultiPage1.Pages(x).Visible = ((x+1) = iPage)
Next x
End If
End Sub
编辑 - @jstola 和我认为相似...
下面的代码假定 Multipage 中 Pages 的 Caption 反映了列表CBO_EntryType
:
Private Sub CBO_EntryType_Change()
Dim iPage As Long
For iPage = 0 To Me.MultiPage1.Pages.Count - 1
With Me.MultiPage1.Pages(iPage)
.Visible = (.Caption = CBO_EntryType.Value)
End With
Next
End Sub
我已经编写了 VBA 代码来根据组合框选择显示隐藏选项卡。组合框中有七个选项,每个选项对应框架中的七个隐藏选项卡。
Private Sub CBO_EntryType_Change()
Dim iPage As Integer
If Me.CBO_EntryType.Value = "Abstracts" Then
iPage = 1
ElseIf CBO_EntryType.Value = "Awards" Then
iPage = 2
ElseIf CBO_EntryType.Value = "Career Fairs" Then
iPage = 3
ElseIf CBO_EntryType.Value = "Editorials" Then
iPage = 4
ElseIf CBO_EntryType.Value = "Rankings" Then
iPage = 5
ElseIf CBO_EntryType.Value = "Tradeshows" Then
iPage = 6
ElseIf CBO_EntryType.Value = "Social Media" Then
iPage = 7
End If
Me.MultiPage1.Pages(iPage).Visible = True
End Sub
我似乎遇到的问题是,如何确保隐藏其他选项卡?由于人们只能单击组合框中的一个选项,但他们可能会误单击一个,然后单击正确的选项。根据组合框中的选定项目,只有一个选项卡应该可见。其他六个应该隐藏。
我想在 sub 的末尾使用 For-Each-Next 循环禁用任何与 iPage 变量不匹配的选项卡,但我很难弄清楚如何处理框架和页面For Each Next 循环。变量声明是什么?
未经测试,因此可能需要稍作调整...
Private Sub CBO_EntryType_Change()
Dim iPage, arrPages, x As Long
arrPages = Array("Abstracts", "Awards", "Career Fairs", "Editorials", _
"Rankings", "Tradeshows", "Social Media")
'find the index in the array...
iPage = Application.Match(Me.CBO_EntryType.Value, arrPages, 0)
'if got a match then loop over the pages and show/hide
If Not IsError(iPage) Then
For x = 0 To Me.MultiPage1.Pages.Count-1
Me.MultiPage1.Pages(x).Visible = ((x+1) = iPage)
Next x
End If
End Sub
编辑 - @jstola 和我认为相似...
下面的代码假定 Multipage 中 Pages 的 Caption 反映了列表CBO_EntryType
:
Private Sub CBO_EntryType_Change()
Dim iPage As Long
For iPage = 0 To Me.MultiPage1.Pages.Count - 1
With Me.MultiPage1.Pages(iPage)
.Visible = (.Caption = CBO_EntryType.Value)
End With
Next
End Sub