ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro that AutoFilters based on Cell value (https://www.excelbanter.com/excel-programming/370841-macro-autofilters-based-cell-value.html)

al

Macro that AutoFilters based on Cell value
 
I want to write a Macro that will run an autofilter based on a cell value.

Ex:
Let's say cell A1 = red and I have a list of cars with their color beside
it. When I run the macro, I want it to use AutoFilter to show all the red
cars. However, if I change A1 to blue, when the macro is run, I want it to
show all the blue cars.

Thanks

Neil Kennedy

Macro that AutoFilters based on Cell value
 
The following line in your macro will do the trick:-

Selection.AutoFilter Field:=2, Criteria1:=colour (colour is the variable for
the value of A1 in)

Change the field number as required - if your date filter is on columns E to
X and the colour is in H then the field number becomes 4 etc

I would recommend putting this line in at the start of the macro:-

Selection.AutoFilter Field:=2

Again, where the field number is your column - this will reset all filters
to show all records.



"Al" wrote in message
...
I want to write a Macro that will run an autofilter based on a cell value.

Ex:
Let's say cell A1 = red and I have a list of cars with their color beside
it. When I run the macro, I want it to use AutoFilter to show all the red
cars. However, if I change A1 to blue, when the macro is run, I want it
to
show all the blue cars.

Thanks




al

Macro that AutoFilters based on Cell value
 
Looks like that'd work, but how do I declare a the cell as a variable?
Sorry, I'm new to writing macros.


"Neil Kennedy" wrote:

The following line in your macro will do the trick:-

Selection.AutoFilter Field:=2, Criteria1:=colour (colour is the variable for
the value of A1 in)

Change the field number as required - if your date filter is on columns E to
X and the colour is in H then the field number becomes 4 etc

I would recommend putting this line in at the start of the macro:-

Selection.AutoFilter Field:=2

Again, where the field number is your column - this will reset all filters
to show all records.



"Al" wrote in message
...
I want to write a Macro that will run an autofilter based on a cell value.

Ex:
Let's say cell A1 = red and I have a list of cars with their color beside
it. When I run the macro, I want it to use AutoFilter to show all the red
cars. However, if I change A1 to blue, when the macro is run, I want it
to
show all the blue cars.

Thanks





Neil Kennedy

Macro that AutoFilters based on Cell value
 
We all had to start somewhere :)

Try something like this:-

Sub Macro1()

Dim colour As String

Selection.AutoFilter Field:=1 ********amend as required

Range("A1").Select
colour = ActiveCell.Value

Selection.AutoFilter Field:=1, Criteria1:=colour ********amend as required

Range("A1").Select

End Sub


"Al" wrote in message
...
Looks like that'd work, but how do I declare a the cell as a variable?
Sorry, I'm new to writing macros.


"Neil Kennedy" wrote:

The following line in your macro will do the trick:-

Selection.AutoFilter Field:=2, Criteria1:=colour (colour is the variable
for
the value of A1 in)

Change the field number as required - if your date filter is on columns E
to
X and the colour is in H then the field number becomes 4 etc

I would recommend putting this line in at the start of the macro:-

Selection.AutoFilter Field:=2

Again, where the field number is your column - this will reset all
filters
to show all records.



"Al" wrote in message
...
I want to write a Macro that will run an autofilter based on a cell
value.

Ex:
Let's say cell A1 = red and I have a list of cars with their color
beside
it. When I run the macro, I want it to use AutoFilter to show all the
red
cars. However, if I change A1 to blue, when the macro is run, I want
it
to
show all the blue cars.

Thanks








All times are GMT +1. The time now is 01:46 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com