【问题标题】:VBA Excel - Userform with comboboxes filter down and writeVBA Excel - 带有组合框过滤器的用户表单并写入
【发布时间】:2016-07-13 19:06:36
【问题描述】:

我正在寻找有关此代码的一些建议。它是一个带有 3 个组合框的用户窗体,第一个组合框过滤 BLOCK(唯一值),第二个过滤标签(也是唯一的),最后一个是 ACT。选择所有 3 个后,我们将 STATUS 写在同一行。

第一个过滤器没问题,但我不知道如何更进一步我无法让自动过滤器在第二个过滤器上工作...有更好的解决方案吗?

在我拥有的代码和表格下方。

谢谢,

Private Sub UserForm_Initialize()

    Dim v, e, lastrow
    lastrow = Sheets("Plan1").Cells(Rows.Count, 1).End(xlUp).Row
    With Sheets("Plan1").Range("A2:A" & lastrow)
        v = .Value
    End With
    With CreateObject("scripting.dictionary")
        .comparemode = 1
        For Each e In v
            If Not .exists(e) Then .Add e, Nothing
        Next
        If .Count Then Me.cbBloco.List = Application.Transpose(.keys)
    End With

End Sub

-

BLOCK        ACT    TAG          STATUS
M00          FAB    201-02-31
M00          MON    201-02-31
M02          FAB    201-02-32
M02          MON    201-02-32
M02          INS    201-02-32
M02          FAB    201-02-33
M02          MON    201-02-33
M02          INS    201-02-33
M02          TER    201-02-33

【问题讨论】:

    标签: excel vba combobox populate


    【解决方案1】:

    编辑在 op 的详细规格之后 编辑 2:在 OP 的新规范之后

    在表单的模块中试试这个

    Option Explicit
    
    Dim cnts(1 To 3) As ComboBox
    Dim list(1 To 3) As Variant
    Dim dataRng As Range, dbRng As Range, statusRng As Range, helperRng As Range
    
    
    Private Sub UserForm_Initialize()
    
    Set dbRng = Sheets("Plan1").UsedRange
    Set helperRng = dbRng.Offset(dbRng.Rows.Count + 1, dbRng.Columns.Count + 1).Cells(1, 1)
    Set dataRng = dbRng.Offset(1).Resize(dbRng.Rows.Count - 1)
    Set statusRng = dataRng.Columns(dbRng.Columns.Count)
    
    With Me
        Set cnts(1) = .cbBloco '<== give control its actual name
        Set cnts(2) = .cbAct '<== give control its actual name
        Set cnts(3) = .cbTag '<== give control its actual name
    End With
    
    Call FillComboBoxes
    
    End Sub
    
    
    Private Sub FillComboBoxes()
    Dim i As Long
    
    Application.ScreenUpdating = False
    
    dbRng.Autofilter field:=4, Criteria1:="<>ISSUED" ' <== added, to avoid rows with "ISSUED" status
    
    For i = 1 To UBound(cnts)
    
        dataRng.SpecialCells(xlCellTypeVisible).Columns(i).Copy Destination:=helperRng
    
        With helperRng.CurrentRegion
            If .Rows.Count > 1 Then .RemoveDuplicates Columns:=Array(1), Header:=xlNo
            With .CurrentRegion
                If .Rows.Count > 1 Then
                    list(i) = Application.Transpose(.Cells)
                Else
                    list(i) = Array(.Value)
                End If
                cnts(i).list = list(i)
                .Clear
            End With
        End With
    
    Next i
    Application.ScreenUpdating = True
    
    End Sub
    
    
    Private Sub ResetComboBoxes()
    Dim i As Long
    
    FillComboBoxes '<== added. since you don't want "ISSUED" rows to be shown, all lists must be refilled
    'For i = 1 To UBound(cnts)
    '    cnts(i).list = list(i)
    '    cnts(i).ListIndex = -1
    'Next i
    
    End Sub
    
    
    Private Sub CbOK_Click()
    Dim i As Long
    
    statusRng.ClearContents
    
    With dbRng
        dbRng.Autofilter field:=4, Criteria1:="<>ISSUED" ' <== added, to avoid rows with "ISSUED" status
        For i = 1 To UBound(cnts)
            .Autofilter field:=i, Criteria1:=cnts(i).Value
        Next i
    
        If .SpecialCells(xlCellTypeVisible).Cells.Count > .Columns.Count Then
            statusRng.SpecialCells(xlCellTypeVisible).Value = "ISSUED"
        Else
            MsgBox "No Match"
        End If
    
        .Autofilter
        dbRng.Autofilter field:=4, Criteria1:="<>ISSUED" ' <== added, to avoid rows with "ISSUED" status
    End With
    
    End Sub
    
    
    Private Sub CbReset_Click()
    Call ResetComboBoxes
    End Sub
    
    
    Private Sub cbAct_AfterUpdate()
        Call UpdateComboBoxes
    End Sub
    
    
    Private Sub cbBloco_AfterUpdate()
        Call UpdateComboBoxes
    End Sub
    
    
    Private Sub cbTag_AfterUpdate()
        Call UpdateComboBoxes
    End Sub
    
    
    Private Sub UpdateComboBoxes()
    
    Dim i As Long
    
    With dbRng
        .Autofilter
        dbRng.Autofilter field:=4, Criteria1:="<>ISSUED" ' <== added, to avoid rows with "ISSUED" status
        For i = 1 To UBound(cnts)
            If cnts(i).ListIndex > -1 Or cnts(i).text <> "" Then .Autofilter field:=i, Criteria1:=cnts(i).Value
        Next i
    
        If .SpecialCells(xlCellTypeVisible).Cells.Count > .Columns.Count Then
            Call RefillComboBoxes
        Else
            Call ClearComboBoxes
        End If
    
        .Autofilter
        dbRng.Autofilter field:=4, Criteria1:="<>ISSUED" ' <== added, to avoid rows with "ISSUED" status
    End With
    
    End Sub
    
    
    Private Sub RefillComboBoxes()
    Dim i As Long, j As Long
    Dim cell As Range
    
    Application.ScreenUpdating = False
    For i = 1 To UBound(cnts)
    
        j = 0
        For Each cell In dataRng.Columns(i).SpecialCells(xlCellTypeVisible)
            helperRng.Offset(j) = cell.Value
            j = j + 1
        Next cell
    
        With helperRng.CurrentRegion
            If .Rows.Count > 1 Then .RemoveDuplicates Columns:=Array(1), Header:=xlNo
            With .CurrentRegion
                If .Rows.Count > 1 Then
                    cnts(i).list = Application.Transpose(.Cells)
                Else
                    cnts(i).list = Array(.Value)
                End If
                .Clear
            End With
        End With
    Next i
    Application.ScreenUpdating = True
    
    End Sub
    
    
    Private Sub ClearComboBoxes()
    
    Dim i As Long
    
    For i = 1 To UBound(cnts)
        cnts(i).Clear
    Next i
    
    End Sub
    

    【讨论】:

    • 但实际上我需要 cbBloco 是唯一值,然后 cbTags 只显示带有 cbBloco.value 的值,cbAct 显示带有 cbTag.value 的值。相反 .removeduplicates 有没有办法过滤值然后复制它?
    • 太棒了!我只是在弄清楚将状态写到搜索行的正确地址时发生了什么。非常感谢 user3598756!
    • 很高兴能提供帮助。如果我回答了你的问题,请标记我的回答。谢谢
    • user3598756,你能再帮我一次吗?此组合选择是向某人发出任务并记录下来。想象一下,我选择块 M02,执行 INS,标记 201-02-32 并发出这个。下次我发出另一个任务,该组合不会再次作为选项出现。怎么办?
    • 是从 dataRng 中删除发布的任务,以便用户无法再选择它,还是只是简单地追踪它以供将来的任务检查,同时仍然允许用户选择它?
    猜你喜欢
    • 2018-03-21
    • 1970-01-01
    • 2011-02-07
    • 2021-06-14
    • 2021-07-11
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    相关资源
    最近更新 更多