Dynamic filter using excel VBA

半世苍凉 提交于 2021-01-29 07:11:16

问题


Hi I am new to using dynamic search boxes to filter in excel. I have a large dataset that I want to be able to type in any value from the a multi column table and all data with that value will be returned.

I have turned the whole dataset into a table and used concat formula at the end of the table. The formula for which is: =CONCAT(Table4[@[Currency Description]:[SUM No. Data Submissions]]& "")

Currency Description being mt first column, SUM No. Data Submissions the last.

This for some reason returns the #value error code I am not sure why.

I think this is why my code is then not working. Theoretically this should allow me to type any text in the cell range "search_string" which should return what I am after. I am not sure why this is not working, any help would be great thanks. The code I have is the following:

Dim KeyCells As Range
Set KeyCells = Range("search_string")

If Not Application.Intersects(KeyCells, Range(Target.Address)) _
Is Nothing Then
FastFilter (KeyCells.Value)

End If


End Sub 


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub 


Sub FastFilter(sch As String)
Dim lo As ListObject
    Set lo = ActiveSheet.ListObjects(4)
    'ListObjects(4) relates to column
    lastcol = lo.ListColumns.Count
    
    If lo.AutoFilter.FilterMode Then
        lo.AutoFilter.ShowAllData
        lo.Range.AutoFilter Field:=lastcol, Criteria1:= _
            Array("*" + sch + "*"), Operator:=xlFilterValues
        Else
        lo.Range.AutoFilter Field:=lastcol, Criteria1:= _
            Array("*" + sch + "*"), Operator:=xlFilterValues
            '* adds wild card search
        End If
    Range("search_string").Select
End Sub ```

来源:https://stackoverflow.com/questions/62883310/dynamic-filter-using-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!