Hi,
Quote:
|
However, I wish to add one thing that when I click Button to match the criteria, sometimes it shows only Part no. (as shown in the file you attached) and sometimes with all relevant field fields (i.e. A Min, A Max, B Min B Max ......so on). I fail to understand the reason for the same.
|
The key to this is the
CopyToRange parameter.
Code:
Sub Button1_Click()
Worksheets("Master").Range("A:I").AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Worksheets("Query").Range("G2:N3"), _
CopyToRange:=Worksheets("Query").Range("R2"), _
Unique:=False
End Sub
In the example I attached, I had
pre-filled in R2 with the field name "PartNo". This will mean that the query will only return results from the "PartNo" field. If you leave R2 blank then the query will return results from all the fields in the table. So, when you set the worksheet up, specify the fields you want to return within the CopyToRange on the worksheet itself and you should be fine.
Quote:
|
One more question, without being copied your Macro, is their any option available to convert into coding when I go for Advance Filter to use List Range, Criteria Range and copy to etc.
|
Depends on what you mean by "convert into coding". The advanced filter CriteriaRange and CopyToRange parameters expect to be passed range object references. You can't pass (for example) string data types into these parameters and you can't create a range object for sole use within VBA, so you have to reference ranges within a worksheet. If you want to avoid using formulas within the criteria range then of course you can construct the criteria strings within VBA and add them to the cells but, either way, you must use a range. Using formulas is fine though - to make it more robust, within the button_click() procedure, you should calculate the criteria range prior to calling the Range.AdvancedFilter() method. This will ensure correct results if the end user has calculations set to manual.
Hope that helps...