我想根据第一个表格中的单个单元格值过滤另一张表格上的表格
您好,在准备新报告时,我遇到了这样的问题:我尝试根据第一个表中的单个单元格值在另一张表上过滤表。我编写的代码似乎有效,但是出于显示要求的结果的考虑,它只突出显示空行。如果我建议使用字符串,则将日期直接放在代码中,一切看起来都很好,但可惜这不是我要查找的内容。有什么建议吗?
Sub Makro3()
Dim ws As Worksheet
Dim tbl As ListObject
Dim mySearch As Variant
Dim sht As Worksheet
Set ws = Arkusz6
Set sht = Arkusz1
Set tbl = ws.ListObjects("ineffective")
'Set tbl = ws.Range("A4:E31")
mySearch = sht.Range("B2").Value
'tbl.Range.AutoFilter Field:=4, Criteria1:="28.06.2019", Operator:=xlAnd
'tbl.Range.AutoFilter Field:=4, Criteria1:="=" & TextBox1.Text & "*", Operator:=xlAnd
'tbl.Range.AutoFilter Field:=4, Criteria1:="*" & TextBox1.Text & "*", Operator:=xlFilterValue
'tbl.Range.AutoFilter Field:=4, Criteria1:="=*" & mySearch & "*", Operator:=xlAnd
End Sub
答案 0 :(得分:0)
这是一个示例,您可以根据另一个工作表上单元格的值自动为日期值过滤表格。在下面的示例中,日期在表格的第2列中。
Sub FilterTable()
Dim ws1 As Worksheet 'sheet with the table
Dim ws2 As Worksheet 'sheet with the search parameter
Dim tbl As ListObject 'table object
Dim mySearch As Variant 'or Dim as Date; cell with the search parameter
Set ws1 = Worksheets("Data")
Set ws2 = Worksheets("Filter Table by Type")
Set tbl = ws1.ListObjects("t_Data")
mySearch = ws2.Range("A1").Value
Debug.Print "mySearch = " & mySearch
With tbl.Range
'~~> Show all records
.AutoFilter Field:=2
If IsDate(mySearch) Then
.AutoFilter Field:=2, _
Operator:=xlFilterValues, _
Criteria1:=mySearch
Else
.AutoFilter Field:=2, _
Operator:=xlFilterValues, _
Criteria1:=DateSerial(Year(mySearch), Month(mySearch), Day(mySearch))
End If
End With
'~~> Release the variables from memory
Set ws1 = Nothing
Set ws2 = Nothing
Set tbl = Nothing
mySearch = vbNull
End Sub