用于更改列表项的Excel VBA组合框宏

时间:2016-04-13 19:44:13

标签: excel vba filter combobox macros

我在工作表“Tables”上创建了一个名为List的IntervieweeName: IntervieweeName named list on Sheet2

在工作表“评估”中,我创建了一个带有IntervieweeName的ListFillRange的组合框。如果与所选IntervieweeName列表项对应的列中存在“是”,则组合框的目的是根据条件自动过滤工作表。以下是“评估”表中的相关数据: Yes/No columns for Sheet "Assessment"

为了完成过滤器,我创建了这个宏,它可以正常工作。

Private Sub cboIntervieweeName_Change()
    Dim IntervieweeName As String

    IntervieweeName = Me.cboIntervieweeName

    If IntervieweeName = "None" Then
        Selection.AutoFilter
    End If
    If IntervieweeName = "Chuck Fox" Then
        ActiveSheet.Range("B6:BL119").AutoFilter _
            field:=17, _
            Criteria1:="Yes", _
            VisibleDropDown:=True
    End If
     If IntervieweeName = "Rob Copeland 1" Then
        ActiveSheet.Range("B6:BL119").AutoFilter _
            field:=19, _
            Criteria1:="Yes", _
            VisibleDropDown:=True
    End If
     If IntervieweeName = "Rob Copeland 2" Then
        ActiveSheet.Range("B6:BL119").AutoFilter _
            field:=21, _
            Criteria1:="Yes", _
            VisibleDropDown:=True
    End If   
  If IntervieweeName = "Rob Copeland 3" Then
        ActiveSheet.Range("B6:BL119").AutoFilter _
            field:=23, _
            Criteria1:="Yes", _
            VisibleDropDown:=True
    End If
End Sub

我还创建了一个按钮宏,将组合框列表项重置为0,即无。

Private Sub resetIntervieweeName_Click()
    cboIntervieweeName.ListIndex = 0
End Sub

这对一个实例很有用,但我的团队希望将其用于未来的项目。如何修改工作表和/或宏以适应更改IntervieweeName列表中的名称而不必修改宏?

这是我的第一个宏。总菜鸟。感谢任何帮助。谢谢。

2 个答案:

答案 0 :(得分:0)

由于您还没有提供任何字段 - 列的关系,因此可能没有多少通用代码。

您当前的代码可以简化为:

Option Explicit

Private Sub cboIntervieweeName_Change()
    Dim lField As Long
    Select Case Me.cboIntervieweeName.Value
        Case "Chuck Fox":       lField = 17
        Case "Rob Copeland 1":  lField = 19
        Case "Rob Copeland 2":  lField = 21
        Case "Rob Copeland 3":  lField = 23
        Case Else
            Selection.AutoFilter
            Exit Sub
    End Select
    ActiveSheet.Range("B6:BL119").AutoFilter field:=lField, Criteria1:="Yes", VisibleDropDown:=True
End Sub

答案 1 :(得分:0)

这里可以放置在UserForm代码窗格中

Option Explicit

Private Sub cboIntervieweeName_Change()
    Dim lField As Long
    Dim dataRng As Range, IntervieweeNameRng As Range, found As Range


    With Worksheets("Assessment")
        Set dataRng = .Range("B6:BL119") '<== here set your "data" range, included first header row above the actual "data"
        Set IntervieweeNameRng = .Range("B5:BL5") '<== here set your "Assessment" sheet range where to search for combobox IntervieweeName selected
    End With

    If cboIntervieweeName.ListIndex > -1 Then

        Set found = IntervieweeNameRng.Find(What:=Me.cboIntervieweeName.Value, LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=True)
        If found Is Nothing Then
            dataRng.AutoFilter  'clear possible previous .Autofilter
        Else
            lField = found.Column
            With dataRng
                .AutoFilter 'clear possible previous .Autofilter
                .AutoFilter field:=lField, Criteria1:="Yes", VisibleDropDown:=True
            End With
        End If
    Else
        dataRng.AutoFilter  'clear possible previous .Autofilter
    End If

End Sub


Private Sub resetIntervieweeName_Click()
    cboIntervieweeName.ListIndex = -1
End Sub

通过这种方式,您可以修改IntervieweeName命名范围的范围和值,并且仍然有cboIntervieweeName组合框&#34;更改&#34;事件处理程序过滤器&#34;评估&#34;相应的表格

我还修改了你的resetIntervieweeName_Click例程,以便&#34;清除&#34; cboIntervieweeName组合框文本将其.ListIndex属性设置为1