VBA – 基于comboboxselect隐藏框架中的页面(标签)

我已经写了VBA代码来使一个隐藏的选项卡出现在一个combobox的select。 combobox中有七个选项,每个选项对应于框架中的七个隐藏选项卡。

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 

我似乎遇到的麻烦是,我如何确保其他选项卡隐藏? 由于人们只能点击combobox中的一个选项,但他们可能会误点击一个选项,然后点击正确的选项。 根据combobox中的选定项目,只能看到一个选项卡。 其他六个应该隐藏。

我正在考虑一个For-Each-Next循环在子结束时禁用任何与iPagevariables不匹配的选项卡,但我很难找出如何在For Each Next循环中寻址框架和页面。 variables声明是什么?

未经testing,所以可能需要稍微调整…

 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和我想象一样…

下面的代码假设 页面中的页面 标题反映了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