我有一个包含5个部分行的表(每周工作时间为1个)。每个行标题都是一个工作人员的名字 - 大约有15个人,所以总共15 * 5行。散布的是标题为" Monday" "星期二"列标题名为"名称"
以下是搜索框的代码:
Sub ScheduleSearch()
'PURPOSE: Filter Data by Staff Name
Dim myButton As OptionButton
Dim MyVal As Long
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Long
Dim DataRange As Range
Dim mySearch As Variant
'Load Sheet into A Variable
Set sht = ActiveSheet
'Unfilter Data (if necessary)
On Error Resume Next
sht.ShowAllData
On Error GoTo 0
'Filtered Data Range (include column heading cells)
Set DataRange = sht.ListObjects("Schedule").Range 'Table
'Retrieve User's Search Input
mySearch = sht.Shapes("WireSearch").TextFrame.Characters.Text 'Control Form
'Loop Through Option Buttons
For Each myButton In ActiveSheet.OptionButtons
If myButton.Value = 1 Then
ButtonName = myButton.Text
Exit For
End If
Next myButton
'Determine Filter Field
On Error GoTo HeadingNotFound
myField = Application.WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
On Error GoTo 0
'Filter Data
DataRange.AutoFilter _
Field:=myField, _
Criteria1:="=*" & mySearch & "*", _
Operator:=xlAnd
'Clear Search Field
sht.Shapes("WireSearch").TextFrame.Characters.Text = "" 'Control Form
'sht.OLEObjects("UserSearch").Object.Text = "" 'ActiveX Control
'sht.Range("A1").Value = "" 'Cell Input
Exit Sub
'ERROR HANDLERS
HeadingNotFound:
MsgBox "The column heading [" & ButtonName & "] was not found in cells " & DataRange.Rows(1).Address & ". " & _
vbNewLine & "Please check for possible typos.", vbCritical, "Header Name Not Found!"
End Sub
Sub ClearScheduleFilter()
'
' ClearScheduleFilter Macro
'
'
ActiveSheet.ListObjects("Schedule").Range.AutoFilter Field:=1
End Sub
现在,当我搜索人员A时,过滤器工作正常,并过滤列#34;名称"由人A。
然而 - 我希望标题为"星期一" "星期二" "星期三" "周四"和"星期五"无论用户搜索什么内容,都会自动包含在此过滤器中。
是否有代码?
更新:以下代码过滤了天和周行,但它不会过滤mySearch值。知道为什么吗?
'Filter Data
DataRange.AutoFilter _
Field:=myField, _
Criteria1:=Array("MONDAY:", "TUESDAY:", "WEDNESDAY:", "THURSDAY:", "FRIDAY:", "WEEK:", " & mySearch & "), _
Operator:=xlFilterValues
答案 0 :(得分:0)
对于name字段,使用Array列表设置包含一周中所有日期和人员的过滤器。
一个基本的shell是这样的:
3 = ncol(df)-1
对于您提供的代码,这应该有效:
Selection.AutoFilter 1, Array("MONDAY", "TUESDAY" , "WEDNESDAY" , "A"), xlFilterValues