Excel VBA Conditional Filter - vba

simple VBA question. I need a VBA subroutine that will filter my data based on a condition (in this case, if what's in column C equals 11-Jun-12 [41071 in numeric form]) without looping through the whole data set. I've looked a bit at filtering on the Internet but nothing seems to be quite what I want (or I just don't really understand what's going on). To be clear, here's an example of what I want:
I want http://imgur.com/qebVv
to go to http://imgur.com/zDncq.
Thanks!

Assuming that the spreadsheet is set up as it appears in your screenshot here is what you can do
Sub DateFilter()
'hide dialogs
Application.ScreenUpdating = False
'filter for records that have June 11, 2012 in column 3
ActiveSheet.Cells.AutoFilter Field:=3, Criteria1:="6/11/2012"
'deletes all matching rows
ActiveSheet.AutoFilter.Range.Delete
Application.ScreenUpdating = True
End Sub

Jack Radcliffe,
Do you mean a simple autofilter, for example:
Sub SimpleColumnDateFilter1()
' Quick Recorded Macro
' Select a Column
' Activate Autofilter
' For a range C1 through the rest of C
' Autofilter such that the column will display dates not equal to 11/15/2012
Columns("C:C").Select
Selection.AutoFilter
ActiveSheet.Range("C:C").AutoFilter Field:=1, Criteria1:= _
"<>11/15/2012", Operator:=xlAnd
End Sub

Related

Excel VB Advanced Filter Copy with Condition

I am trying to put a condition on each row copied. I want all uniques but only if they also have a specific value in another field.
This is what I have to grab all uniques (and it works) but I can't figure out how to get only the rows with a specific value in column J.
r1.Columns(20).AdvancedFilter xlFilterCopy, , Sheet11.Range("A1"), unique:=True
I have tried doing a CriteriaRange but I can't seem to get the syntax correct for it. Additionally I thought about an If statement but logically in my head it means it would fire off the whole list every time it has a true statement, not on a per row basis.
Here is how I thought it might work. But I get a type mismatch error.
r1.Columns(20).AdvancedFilter xlFilterCopy, r1.Columns(10).Value = "November", Sheet11.Range("A1"), unique:=True
Thoughts?
First of all, your Criteria Range should be just that - a Range with the header corresponding to the column to be filtered, and criteria underneath. For example, D1:D2 in this snapshot:
Secondly, you won't be able to copy just a single column (20) while filtering another column (10) in the same step.
You can tweak the Advanced Filter to
First filter the entire list in place based on the criterion provided
And then copy the visible cells in the column in question
Something like this (change Sheet and Range references as needed):
Sub MyFilter()
Dim lastRow As Long
With Sheet1
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A1:B" & lastRow).AdvancedFilter _
Action:=xlFilterInPlace, CriteriaRange:=.Range("D1:D2"), Unique:=True
With .Range("B1:B" & lastRow).SpecialCells(xlCellTypeVisible)
.Copy Sheet2.Range("A1")
End With
.ShowAllData
End With
End Sub
To be able to keep the other parts of the code that worked perfectly. I added a hidden sheet and wrote a macro to copy the filtered results out to the new hidden sheet. Then I ran my original code against the filtered data on that hidden sheet.
Sub FilterLiveToDataSheet()
' Unhide Required Sheets
Sheets("Original-Data").Visible = True
Sheets("Filtered-Data").Visible = True
' Delete Old Data
Sheets("Filtered-Data").Select
Cells.Select
Selection.ClearContents
' Copy Filtered Data
Sheets("Original-Data").Select
Range("TBL_ATTR_Spend[[#Headers],[HeaderName]]").Select
Selection.AutoFilter
ActiveSheet.ListObjects("TBL_ATTR_Spend").Range.AutoFilter Field:=10, _
Criteria1:="Delta"
Cells.Select
Selection.Copy
' Paste to Data Sheet
Sheets("Filtered-Data").Select
Cells.Select
ActiveSheet.Paste
' Unfilter Original Data Page
Sheets("Original-Data").Select
Range("TBL_ATTR_Spend[[#Headers],[HeaderName]]").Select
Selection.AutoFilter
' Hide Required Sheets
Sheets("Original-Data").Visible = False
Sheets("Filtered-Data").Visible = False
' Go to Results Sheet
Sheets("Results").Select

Do Loop + Match

I am looking to delete everything that does not match my inputbox value. However, it seems like the loop I am using is not working at all! It seems the code does not read the loop. The loop should delete the entire row of each cell in the column E that does not match my inputbox variable. I run the code, insert the value in the input box and nothing gets deleted.
Can someone PLEASE help me??
'Get the Tenrox code to be deleted
tenroxcode = InputBox("Insert the Tenrox Code that you want to keep")
'Find and delete all unnecessary tenrox codes
r = Application.Match(tenroxcode, Columns("E"), 0)
Do While IsError(r)
Rows(r).EntireRow.Delete
r = Application.Match(tenroxcode, Columns("E"), 0)
Loop
Try this:
tenroxcode = InputBox("Insert the Tenrox Code that you want to keep")
With Worksheets("Sheet1") ' change as needed
With .UsedRange.Columns(5) 'assumes data is in column a1 and contiguous across cells
If Not .Find(tenroxcode, lookat:=xlWhole) is Nothing Then
.AutoFilter 1, "<>" & tenroxcode
.offset(1).specialcells(xlCellTypeVisible).entirerow.delete 'offset so header row stays
End If
End With
.AutoFilterMode = False
End With
A simple way to do this is to use the Autofilter to filter on anything that doesn't match your condition, and then delete the rows. And I'd suggest turning your data into an Excel Table first, because it simplifies code.
Simply fire up the macro recorder, and you'll get code like this::
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"<>SomeValue", Operator:=xlAnd
Range("Table1[Column1]").Select
Selection.EntireRow.Delete
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1
That should be enough to get you started.

Macro to Filter Data table on multiple criteria based on specific cell value

I tried to record a macro to filter a data table based on the values of 2 cells. It only worked the first time because VBA recorded the cell value instead of the cell reference. I have not been able to figure out how to alter the code to do the following:
Change the filter selection when the cell value changes
have the filter selection change without navigating to the worksheet containing the data table.
The worksheet where the reference cells are located is called "Cost Estimator" and the worksheet with the data table is "AR_BOM"
The table name is "BOM_Table"
Here is the code that was recorded:
Sub Filter_AR_BOM()
'
' Filter_AR_BOM Macro
'
'
Range("E25").Select
Selection.Copy
Sheets("AR_BOM").Select
ActiveSheet.ListObjects("BOM_Table").Range.AutoFilter Field:=4, Criteria1:= _
"Line 11"
Sheets("Cost Estimator").Select
Range("J10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("AR_BOM").Select
ActiveSheet.ListObjects("BOM_Table").Range.AutoFilter Field:=13, Criteria1 _
:="12197118"
Sheets("Cost Estimator").Select
End Sub
Any help is greatly appreciated. I've been trying different things from forums for almost 2 days and don't seem to be making any headway. I'm an extreme macro/vba novice, so I know that doesn't help anything.
Here's a start at least. The code needs to go in the Cost Estimator sheet module
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$25" Or Target.Address = "$J$10" Then
Sheets("AR_BOM").ListObjects("BOM_Table").Range.AutoFilter Field:=4, Criteria1:=Range("E25").value
Sheets("AR_BOM").ListObjects("BOM_Table").Range.AutoFilter Field:=13, Criteria1:=Range("J10").value
End If
End Sub

Autofilter copy paste not all data

I’m trying to have my macro sort column L for DE42, and then copy the visible cells into column AL minus the header Undefined 3, to the last row of the table. I get it to sort but it copies the header and only pastes like half the data. Here’s what I have so far:
Sub DE42 ()
ActiveWorkbook.Sheets(“Alerted Deduped”)
Range (“L1”).AutoFilter Field:=12 Criteria1:=“DE42”
Range(“AL2”).SpecialCell xlCellTypeVisible.Copy
Range.(“F2”).PasteSpecial xlPasteValues
End Sub()
Not sure what you mean by into a last row of a table?
This will simply filter and the copy the visible filter range to AL2 but if the filter is still on then some data may be hidden.
Sub DE42()
With ActiveWorkbook.Sheets("Alerted Deduped")
.Range("L1").AutoFilter Field:=1, Criteria1:="DE42"
.AutoFilter.Range.Offset(1).Resize(.AutoFilter.Range.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy .Range("AL2")
End With
End Sub
Your original filter specified Field 12 so I am wondering if you actually meant to filter A1:L1 on field 12? I will update answer accordingly if so.
Version 2 Assuming data starts column A
Option Explicit
Sub DE42()
With ActiveWorkbook.Sheets("Alerted Deduped")
.Range("A1").AutoFilter
.Range("A1").AutoFilter Field:=12, Criteria1:="DE42"
.AutoFilter.Range.Offset(1, 37).Resize(.AutoFilter.Range.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible).Copy .Range("F2")
End With
End Sub

Using VBA to autofilter Multiple columns, with values from different sheet

I want to use VBA to filter a dump-sheet by 2 columns, with criteria gotten from values on a different sheet in the same workbook.
Used code is:
Sub FilterOnCellValue()
With Sheets("Dump")
.Range("A1:Z10000").AutoFilter Field:=9, Criteria1:=Sheets("ControlPlanning").Range("C1").Value, Field:=23, Criteria1:=Sheets("ControlPlanning").Range("C4").Value
End With
End Sub
For some reason this code filters only one column, while it should be filtering Columns with Number 9 and 23 on 2 different values.
As i want to learn from this, explain my thinking error in this piece of VBA.
Excel version is 2013, if this makes any difference.
Try to seperate the syntax to 2 lines:
Sub FilterOnCellValue()
With Sheets("Dump").Range("A1:Z10000")
.AutoFilter Field:=9, Criteria1:=Sheets("ControlPlanning").Range("C1").Value
.AutoFilter Field:=23, Criteria1:=Sheets("ControlPlanning").Range("C4").Value
End With
End Sub
Use Advanced Filter to use "AND" & "OR" criteria on multiple columns.
If for example you need to filter "columnName1" > 10 or "columnName2" < 10 AND "columnName3" = "Yes"
I would create another worksheet which will contain filter criteria
in the filter sheet enter all the columnnames in one row.
Example: If row 1 has headers then row 2 should contain the values for "AND" condition and row 3 should contain values for "OR" condition.
for "columname1" value on row 2 should be ">10" and "columname2" value on row 3 should be "<10" and "columname3" value on row 2 should be "Yes"
Use Advanced filter like below code in your macro, Change the filter sheet name and range as required.
'Select Sheet and cells which needs to be filtered
Range(Cells(2, 1), ActiveCell.SpecialCells(xlLastCell)).Select
With Selection
.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Sheets("filter").Range("A1:D3"), Unique:=False
End With