隐藏基于userform的行复选框

时间:2013-02-25 16:50:34

标签: excel excel-vba sorting checkbox userform vba

大家早上好,

我有一个我想要对数据进行排序的宏。我的工作簿中的一个按钮调用一个带有10个复选框的小用户窗体。用户应该选择他想要查看的类别,然后单击排序。我想要的结果只是他选择显示的类别,但是我从附加的宏中获得了全部或全部结果。下面是支持表单/按钮对类别进行排序的宏。我搜索了谷歌和其他几个论坛,找不到与我的问题相关的答案!我们将非常感谢您提供的任何帮助。

谢谢!

Private Sub cmdSort_Click()

LastRow = Range("A" & Rows.Count).End(xlUp).Row

If chkFE = True Then
    For Each cell In Range("BC4:BC" & LastRow)
        If UCase(cell.Value) <> "Fire Extinguishers" Then
        cell.EntireRow.Hidden = True
        End If
    Next

End If

If chkChem = True Then
    For Each cell In Range("BD4:BD" & LastRow)
        If UCase(cell.Value) <> "Chem" Then
        cell.EntireRow.Hidden = True
        End If
    Next

End If

If chkFL = True Then
    For Each cell In Range("BE4:BE" & LastRow)
        If UCase(cell.Value) <> "FL" Then
        cell.EntireRow.Hidden = True
        End If
    Next

End If

If chkElec = True Then
    For Each cell In Range("BF4:BF" & LastRow)
        If UCase(cell.Value) <> "Elec" Then
        cell.EntireRow.Hidden = True
        End If
    Next

End If

If chkFP = True Then
    For Each cell In Range("BG4:BG" & LastRow)
        If UCase(cell.Value) <> "FP" Then
        cell.EntireRow.Hidden = True
        End If
    Next

End If

If chkLift = True Then
    For Each cell In Range("BH4:BH" & LastRow)
        If UCase(cell.Value) <> "Lift" Then
        cell.EntireRow.Hidden = True
        End If
    Next

End If

If chkPPE = True Then
    For Each cell In Range("BI4:BI" & LastRow)
        If UCase(cell.Value) <> "PPE" Then
        cell.EntireRow.Hidden = True
        End If
    Next

End If

If chkPS = True Then
    For Each cell In Range("BJ4:BJ" & LastRow)
        If UCase(cell.Value) <> "PS" Then
        cell.EntireRow.Hidden = True
        End If
    Next

End If

If chkSTF = True Then
    For Each cell In Range("BK4:BK" & LastRow)
        If UCase(cell.Value) <> "STF" Then
        cell.EntireRow.Hidden = True
        End If
    Next

End If

If chkErgonomics = True Then
    For Each cell In Range("BL4:BL" & LastRow)
        If UCase(cell.Value) <> "Ergonomics" Then
        cell.EntireRow.Hidden = True
        End If
    Next

End If



Unload frmSort

End Sub

2 个答案:

答案 0 :(得分:2)

您实际上是在过滤,而不是排序。提出问题,为什么不让用户使用Excel的Filter按钮和对话框呢?

要回答您的问题,只有选中一个Checkbox,您的代码才能正常运行。对于每个检查过的复选框,您的代码都隐藏了所有其他类别的行。因此,只有最后一个复选框的类别才会显示

您可以尝试颠倒逻辑。从隐藏的所有行开始,并为单击其类别的任何行设置Hidden = False

答案 1 :(得分:0)

我想分享@AlphaFrog为我提供的解决方案,它完美无缺:

Private Sub cmdSort_Click() 

Dim i As Long, rng As Range, arrCriteria As Variant 

Set rng = Rows(3) 'Headers
arrCriteria = Array("Fire Extinguishers", "Chem", "FL", "Elec", "FP", _ 
"Lift", "PPE", "PS", "STF", "Ergonomics") 

Application.ScreenUpdating = False 
Rows.Hidden = False 
With Range("BC3:BL" & Range("A" & Rows.Count).End(xlUp).Row) 
    For i = 1 To 10 
        If Me.Controls("CheckBox" & i) Then 
            .AutoFilter i, arrCriteria(i - 1) 
            Set rng = Union(rng, .SpecialCells(xlCellTypeVisible).EntireRow) 
            .AutoFilter 
        End If 
    Next i 
    .Parent.AutoFilterMode = False 
    .EntireRow.Hidden = True 
    rng.EntireRow.Hidden = False 
End With 
Application.ScreenUpdating = True 

Unload frmSort 

End Sub

链接到原始答案: http://www.ozgrid.com/forum/showthread.php?t=175539