how can this filter be done in a macro?
Dave,
Try this.
Tables on Sheet1, Data on Sheet2 and Filtered data on Sheet3.
Option Explicit
Sub FilterData()
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim acc_rng As Range, exp_rng As Range
Dim c As Range
Dim lastrow As Long
Dim irow As Long
Dim orow As Long
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
Set ws3 = Worksheets("Sheet3")
With ws1
Set acc_rng = .Cells(2, "A").Resize(10, 1)
Set exp_rng = .Cells(2, "B").Resize(38, 1)
End With
orow = 1
With ws2
lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
For irow = 2 To lastrow '<== change starting row
If Application.And(Not (IsError(Application.Match(.Cells(irow, "B"),
acc_rng, 0))), _
Not (IsError(Application.Match(.Cells(irow, "C"), exp_rng, 0))))
Then
orow = orow + 1
.Cells(irow, "A").EntireRow.Copy ws3.Cells(orow, 1)
End If
Next irow
End With
End Sub
"Dave F" wrote:
I have a table of between 50,000 and 60,000 records from which I have to
extract several hundred records. The following function, filled down a
helper column, is one way of filtering these records (via the Auto-Filter
tool):
=IF(AND(ISNUMBER(MATCH(LEFT(B6,3),Sheet1!$A$2:$A$1 1,0)),ISNUMBER(MATCH(C6,Sheet1!$B$2:$B$39))),"X"," ")
B6 is an account number, whose three left digits signify what type of
account it is. A2:A11 in Sheet1 is the list of those three left digits I
want to extract. C6 is an expense code; B2:B39 in Sheet1 is a list of those
expense codes I want to extract. One would run the AutoFilter on the "X"
values returned by the above formula.
However, what I would like to do is have a macro which runs the same logic,
copies the filtered records, and pastes them in a new sheet. Then all I have
to do is attach that macro to a button.
How to do this?
Thanks for any insight.
Dave
--
A hint to posters: Specific, detailed questions are more likely to be
answered than questions that provide no detail about your problem.
|