Excel VBA AutoFilter Codes

HPM

I want to filter a list of industries by using their industry code (e.g. C10.3, H53.1 etc.)as criteria. At the end, I want my sheet just showing those industries.

The data I want to filter is in Tabelle1 Column 21. Furthermore, I have stated my industry codes on Tabelle3 Column B. However, the following code does not execute.

Does anyone know why and how I can adjust it to my needs?

Sub Autofilter()    
    Dim Bereich As Range
    Dim IndustryCode As Variant

    Set Bereich = Tabelle1.UsedRange
    IndustryCode = Tabelle3.Range("B:B").Value2

    Bereich.Autofilter Field:=21, Criteria1:="=" & IndustryCode  
End Sub
Pᴇʜ

You need to transpose the IndustryCode because this

IndustryCode = Tabelle3.Range("B:B").Value2

results in a 2 dimensional array(1 to 1048576, 1 to 1) but the criteria awaits a 1 dimensional array.

So after

IndustryCode = Application.WorksheetFunction.Transpose(IndustryCode)

you get a 1 dimensional array(1 to 65536) which you can use as Criteria1 together with Operator:=xlFilterValues.

Sub aaa()
    Dim Bereich As Range
    Dim IndustryCode As Variant

    Set Bereich = Tabelle1.UsedRange
    IndustryCode = Tabelle3.Range("B:B").Value2
    IndustryCode = Application.WorksheetFunction.Transpose(IndustryCode)

    Bereich.AutoFilter Field:=21, Criteria1:=IndustryCode, Operator:=xlFilterValues
End Sub

Note that it is not very elegant to use the whole column because the array has 65536 entries which means most are empty. A better way would be using only the range as array that is filled with data:

IndustryCode = Tabelle3.Range("B1", Tabelle3.Cells(Cells.Rows.Count, "B").End(xlUp)).Value2

This will reduce the array to the used part of column B only.


Also note that if IndustryCode (column B) contains true numbers they need to be converted into strings with a loop over the array.

ary(i) = CStr(ary(i))

as illustrated in this answer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Set AutoFilter in Excel with VBA

From Dev

Excel VBA AutoFilter Percentages

From Dev

Excel VBA autofilter all but three

From Dev

Autofilter on Mutliple Columns Excel VBA

From Dev

Excel VBA AutoFilter adds empty rows

From Dev

excel vba - autofilter from selected range

From Dev

VBA Excel autofiltermode = false not turning off autofilter

From Dev

Excel VBA syntax for numeric wildcard in AutoFilter Criteria?

From Dev

Excel VBA copying rows using autofilter

From Dev

Excel VBA add Autofilter if it doesn't exist

From Dev

VBA Autofilter Set by Date in Excel - not showing any data

From Dev

Delete Hidden/Invisible Rows after Autofilter Excel VBA

From Dev

Excel VBA Autofilter issues with multiple inputs per column

From Dev

VBA Excel autofilter for any date before the current month

From Dev

How to transfer criteria from string array to autofilter. VBA Excel

From Dev

excel vba autofilter range and delete all rows with 0 in col D

From Dev

excel VBA - return Criteria1 Array from an Autofilter

From Dev

AutoFilter for a value which has a newline character in between (Excel/VBA)

From Dev

VBA Autofilter Set by Date in Excel - not showing any data

From Dev

Excel Autofilter doesn't work on column with date and time with VBA code

From Dev

How do I loop through an Autofilter using VBA in excel?

From Dev

Excel VBA Runtime Error 1004 ActiveSheet.ListObject.Range.Autofilter

From Dev

excel vba Assign cell value to variable from Autofilter result

From Dev

excel VBA - return Criteria1 Array from an Autofilter

From Dev

VBA Autofilter with variable range

From Dev

VBA Autofilter with variable range

From Dev

Autofilter for multiple fields in VBA

From Dev

Very slow execution of Excel 2016 VBA codes

From Dev

Detect autofilter on Excel Tables

Related Related

  1. 1

    Set AutoFilter in Excel with VBA

  2. 2

    Excel VBA AutoFilter Percentages

  3. 3

    Excel VBA autofilter all but three

  4. 4

    Autofilter on Mutliple Columns Excel VBA

  5. 5

    Excel VBA AutoFilter adds empty rows

  6. 6

    excel vba - autofilter from selected range

  7. 7

    VBA Excel autofiltermode = false not turning off autofilter

  8. 8

    Excel VBA syntax for numeric wildcard in AutoFilter Criteria?

  9. 9

    Excel VBA copying rows using autofilter

  10. 10

    Excel VBA add Autofilter if it doesn't exist

  11. 11

    VBA Autofilter Set by Date in Excel - not showing any data

  12. 12

    Delete Hidden/Invisible Rows after Autofilter Excel VBA

  13. 13

    Excel VBA Autofilter issues with multiple inputs per column

  14. 14

    VBA Excel autofilter for any date before the current month

  15. 15

    How to transfer criteria from string array to autofilter. VBA Excel

  16. 16

    excel vba autofilter range and delete all rows with 0 in col D

  17. 17

    excel VBA - return Criteria1 Array from an Autofilter

  18. 18

    AutoFilter for a value which has a newline character in between (Excel/VBA)

  19. 19

    VBA Autofilter Set by Date in Excel - not showing any data

  20. 20

    Excel Autofilter doesn't work on column with date and time with VBA code

  21. 21

    How do I loop through an Autofilter using VBA in excel?

  22. 22

    Excel VBA Runtime Error 1004 ActiveSheet.ListObject.Range.Autofilter

  23. 23

    excel vba Assign cell value to variable from Autofilter result

  24. 24

    excel VBA - return Criteria1 Array from an Autofilter

  25. 25

    VBA Autofilter with variable range

  26. 26

    VBA Autofilter with variable range

  27. 27

    Autofilter for multiple fields in VBA

  28. 28

    Very slow execution of Excel 2016 VBA codes

  29. 29

    Detect autofilter on Excel Tables

HotTag

Archive