1

大家,早安,

我有一个要对数据进行排序的宏。我的工作簿中的一个按钮调用一个带有 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
4

2 回答 2

2

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

Checkbox要回答您的问题,您的代码只有在被选中的情况下才会起作用。对于每个选中的复选框,您的代码都隐藏了所有其他类别的行。所以只有最后一个 Checkbox 的类别才会显示行

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

于 2013-02-25T18:38:14.783 回答
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

于 2013-02-26T14:39:17.907 回答