首页 文章

VBA - 根据Combobox选择隐藏框架中的页面(选项卡)

提问于
浏览
0

我已经编写了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循环中寻址框架和页面 . 变量声明是什么?

2 回答

  • 1

    未经测试,可能需要进行细微调整......

    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和我一样......

  • 1

    下面的代码假定 Multipage 中的 PagesCaption 反映了 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
    

相关问题