[英]VBA Filter Multiple Columns At Same Time
Jim L from Ontario was a tremendous help in solving my first challenge.来自安大略省的 Jim L 在解决我的第一个挑战方面提供了巨大的帮助。 You can review that at this link : Previous Discussion
您可以在此链接中查看: 以前的讨论
I thought it would be a simple matter to add filters for additional columns once the DATE FILTER QUESTION was solved.我认为一旦解决了 DATE FILTER QUESTION,为其他列添加过滤器将是一件简单的事情。 Nope.
不。
I've tried adding additional filters within the same confines as the DATE filter ... I've tried adding the additional filters in the same sub but below the DATE filter ... even placing the additional filters in separate subs.我已经尝试在与 DATE 过滤器相同的范围内添加额外的过滤器......我已经尝试在同一个子中但在 DATE 过滤器下方添加额外的过滤器......甚至将额外的过滤器放在单独的子中。 Nothing is working.
没有任何工作。
The example workbook may be downloaded here : Download Workbook示例工作簿可在此处下载: 下载工作簿
The end users will have a need to filter on one or more columns at the same time.最终用户需要同时过滤一个或多个列。 How can I work that in with the existing code in the workbook ?
我如何使用工作簿中的现有代码进行处理?
I'm stumped !我难住了!
Thanks.谢谢。
Try adding the auto-filter across all the columns and then use each button to set the criteria for one column only.尝试在所有列中添加自动过滤器,然后使用每个按钮只为一列设置条件。 Here is an example for the fist 3 columns that you can expand to the others.
这是您可以扩展到其他列的前 3 列的示例。
COL_FILTER is an integer parameter to the sub filterCol which is generic to all the columns you want to add a filter to (except the date which is a special case). COL_FILTER 是 sub filterCol 的整数参数,它是您想要添加过滤器的所有列的通用参数(日期除外,这是一种特殊情况)。 Assign your "UNIT" filter button to the sub FilterB, "Call RCVD" button to sub FilterC etc. When you first press any button the filter drop downs appear across all columns but only 1 column will have criteria applied.
将“UNIT”过滤器按钮分配给子过滤器B,将“调用RCVD”按钮分配给子过滤器C 等。当您第一次按下任何按钮时,过滤器下拉列表会出现在所有列中,但只有1 列将应用条件。 Pressing further buttons will set criteria for those additional columns and retain the previous filters.
按更多按钮将为这些附加列设置条件并保留以前的过滤器。 Entering a blank search term will remove the criteria for that column only
输入空白搜索词将仅删除该列的条件
Option Explicit
Sub ResetFilters()
Dim Wks As Worksheet
Set Wks = Sheets("Call Log File")
With Wks
On Error Resume Next
If Wks.AutoFilterMode Then
Wks.AutoFilterMode = False
End If
End With
End Sub
Sub FilterB()
Call filterCol(2)
End Sub
Sub FilterC()
Call filterCol(3)
End Sub
Sub filterCol(COL_FILTER As Integer)
Dim wb As Workbook, ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("Call Log File")
' set auto filter to all columns if not already on
Dim rngFilter As Range
Set rngFilter = ws.Range("A2:K2")
If ws.AutoFilterMode = False Then
rngFilter.AutoFilter
End If
'Debug.Print rngFilter.Address
' get filter criteria
Dim sColname As String
sColname = ws.Cells(2, COL_FILTER)
Dim sPrompt As String, sUserInput As String, n As Integer
sPrompt = "Enter " & sColname
sUserInput = InputBox$(sPrompt)
Dim criteria(2) As String
criteria(1) = "*" & sUserInput & "*"
' apply filter to the select column
If ws.AutoFilterMode = True Then
rngFilter.AutoFilter COL_FILTER, "=" & criteria(1)
End If
End Sub
Sub FilterDate()
Const COL_FILTER As Integer = 1 ' A
Dim wb As Workbook, ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("Call Log File")
' set auto filter to all columns if not already on
Dim rngFilter As Range
Set rngFilter = ws.Range("A2:K2")
If ws.AutoFilterMode = False Then
rngFilter.AutoFilter
End If
'Debug.Print rngFilter.Address
Dim sPrompt As String, sUserInput As String, n As Integer
sPrompt = "Enter DATE" & vbCrLf & _
"For YEAR ONLY: YY" & vbCrLf & _
"For YEAR & MONTH: YYMM" & vbCrLf & _
"For YEAR & MONTH & DAY: YYMMDD"
sUserInput = InputBox$(sPrompt)
n = Len(sUserInput)
If n = 0 Then
rngFilter.AutoFilter COL_FILTER ' clear filter
Exit Sub
ElseIf Not (n = 2 Or n = 4 Or n = 6) Then
MsgBox sUserInput & " is not correct", vbExclamation, "Wrong Format"
Exit Sub
End If
Dim mydate As Variant
mydate = dateRange(sUserInput)
'Debug.Print sUserInput, mydate(1), mydate(2)
If ws.AutoFilterMode = True Then
rngFilter.AutoFilter COL_FILTER, ">=" & mydate(1), 1, "<=" & mydate(2)
End If
End Sub
Function dateRange(s As String)
Dim s1 As String, s2 As String
s1 = "000"
s2 = "999"
Select Case Len(s)
Case 2
s1 = "0101" & s1
s2 = "1231" & s2
Case 4
s1 = "01" & s1
s2 = "31" & s2
Case 6
' nothing to add
Case Else
dateRange = ""
Exit Function
End Select
Dim rng(2) As Long
rng(1) = CLng(s + s1)
rng(2) = CLng(s + s2)
dateRange = rng
End Function
Fo the benefit of others ... this following macro will search for a term in Col B, after the table was filtered by Col A. Although this is not a "filtering approach" in Col B, it is very effective and does precisely what I was looking for.为了其他人的利益......下面的宏将在表被 Col A 过滤后搜索 Col B 中的一个术语。虽然这不是 Col B 中的“过滤方法”,但它非常有效并且精确地执行我正寻找。
Thank you to all for your assistance.感谢大家的帮助。
Sub FilterB()
Dim cl As Range, rng As Range
Dim sPrompt As String, sUserInput As String
Set rng = Range("B3:B100")
sPrompt = "Enter SEARCH TERM"
sUserInput = InputBox$(sPrompt)
For Each cl In rng.SpecialCells(xlCellTypeVisible)
If cl.Value <> sUserInput Then
cl.Rows.Hidden = True
End If
Next cl
End Sub结束子
And this can be duplicated as many times as required to further "filter down" additional columns.这可以根据需要重复多次,以进一步“过滤”其他列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.