Autofilter parts of multiple arguments - excel

I'm trying to use the AutoFilter function in VBA.
I have cells in this column filled with values that start with a value that I want to filter on like "pathogenic", "polymorfism", "VUS",... but ends with some more text that I don't want to use in the filter.
e.g.
variant
class
V600E
pathogenic variant - BRAF
V590A
VUS - D1234567
G12D
pathogenic variant - KRAS
G15A
polymorfism
A56D
polymorfism - KFP
E4654M
presumably pathogenic variant
In this case I want to filter all rows where the class starts with "pathogenic variant","VUS" and "presumably pathogenic variant", in the example row 1-3 and row 6.
I found a way to use the autofilter for 2 values:
ActiveSheet.Range("$A$2:$BB$550").AutoFilter _
Field:=17, _
Criteria1:=("=patho*"), _
Operator:=xlOr, _
Criteria2:=("=VUS*"), _
Operator:=xlFilterValues
I can't use more then 2 criteria. When I use the following code the filtering doesn't work.
ActiveSheet.Range("$A$2:$BB$550").AutoFilter _
Field:=17, _
Criteria1:=Array("=patho*", "=VUS*", "=presumably patho*"), _
Operator:=xlFilterValues

You can use Advanced Filter.
Columns A and B: Your range
Column D: Criteria
Column F and G: Extract
You you want to Filter in Place instead of using Copy uncomment the last line.
Sub AdvFilter()
Dim yourRange As Range
Dim criteriaRange As Range
Dim extractRange As Range
Set yourRange = Range("A1:B25")
Set criteriaRange = Range("D1:D4")
Set extractRange = Range("F1:G1")
' Advanced filter: Copy
yourRange.AdvancedFilter Action:=xlFilterCopy, criteriaRange:=criteriaRange, CopyToRange:=extractRange, Unique:=False
' Advanced filter: Filter in Place
'yourRange.AdvancedFilter Action:=xlFilterInPlace, criteriaRange:=criteriaRange, CopyToRange:=extractRange, Unique:=False
End Sub

Related

Apply filter based on a character in a string column in VBA

so I am trying to copy unique values from a column after applying several filters to it. For one of the filter I cant filter by exact string because the column is like this:
I want the values which have "U" in 8th position from right and want to ignore the ones which have "O" in the same place.
So for example, in the image: 'S20-0260-TA-002_W06_05C_AMBRH_U-404728' should be present but 'S20-0260-TA-001_W06_30C_75RH_O-404713' shouldn't be.
I know how to check a character from right, from the RIGHT function but I am struggling to apply it in a filter with if condition.
This is my relevant code so far with other filters:
Dim uniquesArray As Variant
Dim uniqueCond As Variant
Dim lastCondRow As Long
Dim lastRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("New Data")
lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Set DR = ThisWorkbook.Sheets("Test")
With ThisWorkbook.Sheets("Test")
'Find unique Condition
Worksheets("New Data").Range("$B$1:$X$9999").AutoFilter Field:=10, Criteria1:= _
"Assay by HPLC"
Worksheets("New Data").Range("$B$1:$X$9999").AutoFilter Field:=15, Criteria1:= _
"(Average)"
ws.Range("I1:I9999").SpecialCells(xlCellTypeVisible).Copy
.Cells(1, 27).PasteSpecial
.Columns("AA:AA").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("AB1"), Unique:=True
lastCondRow = .Cells(.Rows.Count, "AB").End(xlUp).Row
uniqueCond = .Range("AB2:AB" & lastCondRow)
.Columns("AB").ClearContents
.Columns("AA").ClearContents
End With
I want to apply this filter as well below the 2 auto-filters under the find unique comment. The column number is F, please help
Alternative Option
Offering this alternative, mainly to demonstrate how to apply multiple filters, and one method of applying criteria using wildcards to find exact location of specific characters. This uses a 3 step approach:
• Apply all the filters
• Copy the Data
• Remove duplicates – leaving unique values only
All the action takes place on the “New Data” sheet, and you can change this and the various columns referred to as you wish. Provided for demonstration purposes only.
Option Explicit
Sub testCopyUnique()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("New Data")
Application.ScreenUpdating = False
'Apply all filters and copy the visible cells
With ws.Cells(1, 2).CurrentRegion
.AutoFilter 10, "Assay by HPLC"
.AutoFilter 15, "(Average)"
.AutoFilter 5, "*U???????" '<~~ ”*” = any number of characters, “?” = one character
ws.Range("I:I").SpecialCells(xlCellTypeVisible).Copy ws.Range("AA1")
.AutoFilter
End With
'Remove the duplicates - leaving unique comments only
ws.Range("AA1:AA" & ws.Range("AA" & Rows.Count).End(xlUp).Row).RemoveDuplicates _
Columns:=1, Header:=xlYes
Application.ScreenUpdating = True
End Sub

VBA reference to named range

I have following code in which I have tried to applied 3 filters, now what I want is, to apply this filter criteria based on named range
I have stored relevant value in specific cell and given names to that range
for first criteria i.e. >=200 named range is VOLUME
for second criteria i.e. >=0.07 named range is MOVE
for third criteria i.e. >=400 named range is STRENTH
So what are the changes that I need to make in following code?
Sub FILTER2()
'FILTER CRITERIA
ActiveSheet.Range("$A$2:$AC$500").AutoFilter Field:=6, Criteria1:=">=200", _
Operator:=xlAnd
ActiveSheet.Range("$A$2:$AC$500").AutoFilter Field:=21, Criteria1:=">=0.07" _
, Operator:=xlAnd
ActiveSheet.Range("$A$2:$AC$500").AutoFilter Field:=28, Criteria1:=">=400" _
, Operator:=xlAnd
Range("A1").Select
End Sub
As per my comment, you might want to consider a With statement, better not to refer to ActiveSheet
Sub FILTER2()
With Sheet1 'Change according to your sheets CodeName
.Range("$A$2:$AC$500").AutoFilter 6, ">=" & .Range("VOLUME")
.Range("$A$2:$AC$500").AutoFilter 21, ">=" & .Range("MOVE")
.Range("$A$2:$AC$500").AutoFilter 28, ">=" & .Range("STRENTH")
End With
End Sub
you can try defining a variable to contain the value of your named range.
Named Range in your sheet as "Volume" then i can store that value in var namedRange1.
example below:
Sub FILTER2()
namedRange1 = range("Volume")
'FILTER CRITERIA
ActiveSheet.range("$A$2:$AC$500").AutoFilter Field:=6, Criteria1:=">=" & namedRange1, _
Operator:=xlAnd
....same with the other lines, just define new variable to contain new named range

AutoFilter Criteria Range Not taking an Array

1. I have data in one sheet and want to filter the list of criteria in another column
2. I want to have the criteria in separate WS as well
I have included 2 separate codes below
code for same sheet list Array
Sub FilterTeams()
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.Range("A1:H" & LastRow).AutoFilter Field:=3, Criteria1:="=*" & Range("Q1") & "*"
End Sub
list array in other sheet
code details
Sub Filter()
Dim Criteria As Variant
Criteria = Worksheets("Sheet1").Range("A1:A140")
Worksheets("AP").Range("$A$1:$h$100").AutoFilter Field:=3, Criteria1:=Criteria, Operator:=xlFilterValues
End Sub
it is filtering only the first value and not all values in the column
(it should filter all values contains)
Problem:
You are defining your array from a Range, that gives you a 2D array, which you cannot pass into Autofilter Easily. So we have to change it into 1D Array.
Solution: Change your second code to this:
Sub Filter()
Dim Criteria As Variant
Dim cri() As String
Criteria = Worksheets("Sheet1").Range("A1:A140")
ReDim Preserve cri(UBound(Criteria))
For I = LBound(Criteria) To UBound(Criteria)
cri(I) = Criteria(I, 1)
Next
Worksheets("AP").Range("$A$1:$h$100").AutoFilter Field:=1, Criteria1:=cri, Operator:=xlFilterValues
End Sub
Alternative: The code that you are using is Fixed Code. There is one Dynamic code that could be also of use for you. Check this Answer.

VBA AutoFiltering using multiple variable

I have a problem using my autofilter. I need to filter sever values at a time.
I have the list of values on first sheet (column I2:I50) and on the second sheet I would need to filter all these values from database.
My code so far is:
Dim OZ As Variant
OZ = Array("100001", "100015")
Sheets("Sheet2").Select
Range("A1:M1").AutoFilter Field:=3, Criteria1:=OZ, _
Operator:=xlFilterValues
This works perfectly so far. Now I am strugeling to setup the macro in a way, so the array refers to the Range of Sheet1 and I2:I50. Values in this range are variables calculated by functions.
Can you please help me with that?.
Thanks
This For would set your OZ variable with all the 49 values from Sheet1
'You need 49 values I2:I50
Dim OZ(49) As Variant
For i = 0 To 48
'1st data row starts at I2, hence + 2
OZ(i) = "=" & Worksheets("Sheet1").Range("I" & i + 2).Value
Next i
Worksheets("Sheet2").Range("A1:M1").AutoFilter Field:=3, Criteria1:=OZ, Operator:=xlFilterValues
you could use Join() and Split() functions to return a Variant array compatible for Criteria1
Dim OZ As Variant
OZ = Split(Join(Application.Transpose(Worksheets("Sheet1").Range("I2:I50").Value), "|"), "|")
Worksheets("Sheet2").Select
Range("A1:M1").AutoFilter Field:=3, _
Criteria1:=OZ, _
Operator:=xlFilterValues
but you should avoid the Select/Selection/Activate/ActiveXXX pattern and use explicitly fully qualified range references
Worksheets("Sheet2").Range("A1:M1").AutoFilter Field:=3, _
Criteria1:=Split(Join(Application.Transpose(Worksheets("Sheet1").Range("I2:I50").Value), "|"), "|"), _
Operator:=xlFilterValues
i think that advanced filter will do the job.
Sub ExampleCopy()
Sheets("Table").Range("A1:B7").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("Cryteria").Range("A1:15"), _
CopyToRange:=Sheets("Result").Range("A1"), _
Unique:=False
Sheets("Result").Select
End Sub
'OR
Sub ExampleInPlace()
Sheets("Table").Range("A1:A11").AdvancedFilter Action:=xlFilterInPlace _
, CriteriaRange:=Sheets("Cryteria").Range("A1:A6"), Unique:=False
End Sub
The first one will copy result to a custom location. In this case to Sheet "Result" cell A1.
The second one will filter in place so in sheet "Table"
Bear in mind that in order for this to work your cryteria column header has to be the same as the data table column.
You can aslo try recording this macro using "advanced" from ribbon tab "DATA" section "Sort&filter".
Good luck.
The range can be evaluated to an array:
[Sheet2!A1:M1].AutoFilter 3, [transpose(text(Sheet1!I2:I50, "#"))], xlFilterValues

Excel Filter Data with an array using variables

Good day.
The following code does what I want, i.e. correctly filters my data.
ActiveSheet.Range("$A$1:$AV$791").AutoFilter Field:=5, Criteria1:=Array( _
"Delta", "Ladner", "Tsawwassen"), Operator:=xlFilterValues
But, I want to use variables to change and filter different cities.
The following code gives me the accompanying string.
cityStr = """Delta""" & ", " & """Ladner""" & ", " & """Tsawwassen"""
returns the string: "Delta", "Ladner", "Tsawwassen"
But when I put the variable into the filter statement it doesn't do anything. It doesn't matter if I use quotes around cityStr - neither works.
ActiveSheet.Range("$A$1:$AV$791").AutoFilter Field:=5, Criteria1:=Array( _
cityStr), Operator:=xlFilterValues
Any suggestions on how to use a variable in an array?
Thanks
Les
It wants an array, so give it an array
Sub test()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim rng As Range
Set rng = ws.Range("A1:A10")
Dim CityStr() As Variant
CityStr() = Array("Delta", "Ladner", "octopus")
rng.AutoFilter field:=1, Criteria1:=CityStr(), Operator:=xlFilterValues
End Sub
Declare a variant array, fill it with your strings, and use it as criteria1. I think you have to use a variant per kb.

Resources