Need help filtering a spreadsheet using a macro based on criteria in a cell

dieroo

New Member
Joined
Dec 11, 2015
Messages
3
Hi all, I'm currently working on a spreadsheet and I need to be able to filter it based on state (theres only about 6). I built a different macro for each state that automatically filters like if you were to do it manually. My plan was to chain together a few IF statements in a cell to execute the macro I wanted. In my mind it was going to look something like, "IF(A1=CA,"RunMacroCA",IF(....)). I can't figure out the language I need to get the macro to run, if its even possible. Any help is greatly appreciated!

If this isn't the correct way to go about it, how can I automatically filter based on a given value in a cell?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Welcome to the Board...

What version of Excel are you using? and, how have you set up your "filtering process" -- auto-filtering or advanced filtering?
 
Upvote 0
Welcome to the Board...

What version of Excel are you using? and, how have you set up your "filtering process" -- auto-filtering or advanced filtering?


Hi Jim!
I'm running excel 2013 and am not using the advanced filtering option. I simply clicked the dropdown box in the "State" column and unchecked all then reselected the specific state desired for each macro. They have been tested and are certainly functional so far.
Thanks again.
 
Upvote 0
Welcome to the MrExcel board!

1. How does the value (eg "CA") get into cell A1? does the user enter it, or is there a formula in A1? If a formula, what is the formula?

2. Where is the data that is to be filtered?
 
Upvote 0
Welcome to the MrExcel board!

1. How does the value (eg "CA") get into cell A1? does the user enter it, or is there a formula in A1? If a formula, what is the formula?

2. Where is the data that is to be filtered?

1) The value is entered manually by the user. No formula is neccesary because the user will enter specific criteria.

2) The data that is to be filtered is on the same sheet, within the same worksheet only a few cells away.
 
Upvote 0
2) The data that is to be filtered is on the same sheet, within the same worksheet only a few cells away.
That is very non-specific so you will need to modify this code to match the column your states are in (I've used column D). However, your answer to 1. means that you only need one set of code. To implement the code below,


1. Right click the sheet name tab and choose "View Code".

2. Copy and Paste the code below into the main right hand pane that opens at step 1.

3. Close the Visual Basic window & test by entering, changing/deleting a value in A1

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("A1")) Is Nothing Then
    If Range("A1").Value = "" Then
      Range("D1", Range("D" & Rows.Count).End(xlUp)).AutoFilter Field:=1
    Else
      Range("D1", Range("D" & Rows.Count).End(xlUp)).AutoFilter Field:=1, Criteria1:=Range("A1").Value
    End If
  End If
End Sub
 
Upvote 0
Sample Data...


Excel 2012
ABCD
1Y<< Enter Criteria Here (Either Y or N)
2
3
4AmtNameGoing?Date
5123.00HowardY7/15/2003
6234.00BruceN7/28/2003
7345.00RonN8/31/2003
8456.00HowardN8/1/2003
9567.00RonY9/5/2003
10678.00JoeN6/30/2003
11347.00HowardY7/29/2003
12456.00RonN9/10/2003
Sheet1


Code:
Sub Foo()
Dim Rng As Range
    Set Rng = Range("A4").CurrentRegion
    ActiveSheet.AutoFilterMode = False
    Rng.AutoFilter Field:=3, Criteria1:="=" & ActiveSheet.Range("A1").Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top