Hello Every One,
This time I have come up with another VBA trick which could be used as a replacement for traditional Index(table,Small(if(Column = Criteria, RowNum),nth)) constructions. This is much efficient than any way around based on Excel general Functions like I have stated earlier. So, for the sake explanation, there are two sheets. One is a Master Sheet which would contain Data in the form of a table having different Headings which could be used as criteria either individually or otherwise. Second sheets is an Extract sheet where data would be pulled from Master sheet based on Some criteria placed in the Extract sheet itself.
here are some pictures of Master sheet layout and Extract Sheet.
Here is the code which pulls the records from master sheet based on Town - New York Criteria.
Sub AdvFil()
'Declaring Variables
Dim mRng As Range 'Master Table Range
Dim cRng As Range 'Criteria Range
Dim dRng As Range 'Destination range
Set mRng = Sheets("Master").Range("A1").CurrentRegion 'List Range
Set cRng = Sheets("Extract").Range("F1").CurrentRegion 'Criteria Range
Set dRng = Sheets("Extract").Range("A1") 'Destination cell
dRng.CurrentRegion.Clear 'Clearing Previous Content
'Using Advanced filter to Pull rows
mRng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=cRng, _
CopytoRange:=dRng, _
Unique:=False
dRng.CurrentRegion.Columns.AutoFit 'Auto fitting columns
End Sub
The code is just replicating the manual advanced filter process performed through Excel interface. While using excel interface, It demands List Range ( represented by mRng ), Criteria Range ( represented by cRng in the Code and a Copy to Range ( represented by dRng ).
Note:- Its important that the Column heads (Town) in the Criteria Range (Extact!F1:F2) should exactly match with Column heads in the List Range Or Master Sheet.
you can then assign this macro to a Button and extract records by clicks.
the Criteria Range has plenty of applications which you could find in the links below.
http://www.contextures.com/xladvfilter01.html
http://www.contextures.com/xladvfilter02.html
This really is powerful tool which can handle huge rows of data easily.
Now its your time to give it a try and learn.
Regards,
Vikas Gautam
Comments
Post a Comment