Search TeachExcel.com
TeachExcel.com
TE
Teach Excel MS Office Tutorials Excel Consulting Services Excel Forum
Excel Video Tutorials Excel Tips Free Excel Macros Excel Help Resources Contact TeachExcel
Video Tutorials
  • Free Macros
  • Excel Help Directory
  • Excel 2007 Resources
  • Keyboard Shortcuts
  • Excel Forum
  • Contact/About

Macros
Excel Tutorials For Macros

Filter Data in Excel to Display Results that Contain 1 of 2 Possible Values - AutoFilter


Bookmark and Share

This Excel macro filters data in Excel to display results that contain 1 of 2 possible values. This macro uses the xlor operator and that means that a result can contain one value OR another value. This is a great macro to use to narrow long lists or data sets down to compare two separate items side by side.

To use this macro simply replace the "Enter Criteria Here" text with your own criteria and update the field number. This is a pretty easy to use and yet powerful feature in Excel and in Excel macros.


How Filter Macros Work

All of the elements below often appear within autofilter macros.

Range
    This should be the start of the data set, table, or list which you would like to filter. This can also be the entire range reference to the table. For example, if the table was from cell A1:D450, you could put that as the range or you could put A1 as the range. You can do this because the autofilter feature in Excel will automatically try to determine the total range which you would like to filter.
Field
    This is the number of the column within the data table that you would like to filter. The first field (1) is the very first column in the data set that will be filtered. This means that if your data starts in column B and your range is Range("B1") and you want to filter based on column D, you would put 3 in for the field.
Criteria
    This is the criteria by which you would like to filter. Some of the macros have symbols within the quotation marks after this argument and those symbols (such as <,>,*,?, etc.) should be left where they are in order to retain the functionality of the macro.
Operator
    You will not have to change this. This is simply the way to perform different types of filter features in Excel.
Where to install the macro:  Module

Excel Macro to Filter Data in Excel to Display Results that Contain 1 of 2 Possible Values - AutoFilter

Sub AutoFilter_in_Excel_OR_Operator_One_Field()

Range("A1").AutoFilter Field:=1, Criteria1:="Enter Criteria Here", Operator:=xlOr, Criteria2:="Enter Criteria Here"

End Sub


Bookmark and Share


How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

      For Excel Versions Prior to Excel 2007
      Go to Tools > Macros > Visual Basic Editor

      For Excel 2007
      Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic

  4. On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

    1. Go to the menu at the top of the window and click Insert > Module
    2. Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
    3. Go to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Go to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
    5. Go to Step 8.

  8. Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.

  9. You are now ready to run the macro.



Similar Helpful Excel Resources

Excel Vba: Display Excel Autofilter Criteria. Show Auto Filter Criteria In Excel - Excel

View Content
This topic on Ozgrid provides just what I need, but limited to only 2 criteria - my VBA is not up to extending the code to the ten criteria that I need. Can anyone point me in the right direction please? VB:

Function AutoFilter_Criteria(Header As Range) As String  Dim strCri1 As String, strCri2 As String        Application.Volatile            With Header.Parent.AutoFilter          With .Filters(Header.Column - .Range.Column + 1)                        If Not .On Then Exit Function                                strCri1 = .Criteria1              If .Operator = xlAnd Then                  strCri2 = " AND " & .Criteria2              ElseIf .Operator = xlOr Then                  strCri2 = " OR " & .Criteria2              End If                        End With      End With                AutoFilter_Criteria = UCase(Header) & ": " & strCri1 & strCri2  End Function 


If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines



Use Autofilter To Filter Results Using Criteria From Cell Value In Another Worksheet - Excel

View Content
I am trying to use VBA code to filter data. The filter criteria is based on the cell values in another worksheet. The code works when my criteria only includes less than and greater than but when i try to include 'is equal to' or greater than the code doesn't filter like I need it to.

Here's the code:
VB:

ActiveSheet.AutoFilterMode = False 
Range("A1:X1").AutoFilter 
Range("A1:x1").AutoFilter field:=1, Criteria1:=ThisWorkbook.Worksheets("CONWAY").Range("B1").Value, _ 
Operator:=xlAnd 
Range("A1:D1").AutoFilter field:=2, Criteria1:="=" & ">" & ThisWorkbook.Worksheets("CONWAY").Range("C1").Value 'greater than filter to Contract ("B2" as criteria)
Range("A1:x1").AutoFilter field:=3, Criteria1:="=" & "<" & ThisWorkbook.Worksheets("CONWAY").Range("C1").Value 'less than filter to Op Code ("C2" as criteria)


If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines



Display Source Data From Pivot Tables (field To Filter Has Too Many Unique Values) - Excel

View Content
Excel 2007 compatability mode for 2003- I have a list tab with text values in column C "ID" and a tab with a pivot table. The pivot table had 500,000 rows of underlying data. Based on the cell values of "ID" in column C(in each row), I would like to be able to see the underlying data of the pivot table for only the rows of underlying data where the uderlying data field "ID" matches the "ID" value in column C.

Note that the pivot table can not use the field "ID" becuase there are too many values and it can not represent those.

Also note that I need to be backward compatable to 2003.

I tried a hyperlink but am not sure how to reference the underlying data or how to programatically spawn the results.

Custom Filter Doesn't Display Results - Excel

View Content
Hi,

I am trying to use a custom filter in say Column A. I have a number that begins with 5 but may have any number of digits after that. I tried using a wildcard after the 5 but it does not return any results. I tried using Text to Columns but that didn't help either. What would be the root cause that it would not work. I have several bits of "trial" information for example 51 555 5500 5000 and nothing seems to work when I put even "contains" 5* or "begins with" 5* or equals 5* none of those work. Can anyone help me out. Thanks for your help in advance!

Need To Filter Results, Add Results And Group The Results In Excel 2007 - Excel

View Content
Hello All,

First time poster. I have a large Excel Spreadsheet that has 32 columns and over 50,000 lines of data. Column U has a 4 digit code. There are over 100 different codes in column U. I want Excel to create a new worksheet where each code is only listed once but the contents are summated. Might be easier to show an example.

Here is how the data appears now:

Column U Column X

AAAA $10
AAAA $20
AAAA $50
AAAA $10
AAAA $100
BBBB $20
BBBB $10
BBBB $50
CCCC $80
CCCC $90

Here is the format I want excel to create:

AAAA $190
BBBB $80
CCCC $170

Thanks, Bill

Paste Values Of Auto Filter Results - Excel

View Content
I have the following code that copies only the visible cells in an auto filter...how could I modify this code to paste only the values and not the format?

Code:

.Range("a:a").AutoFilter Field:=1, Criteria1:="True"
    .Range("b:d,g:r").SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("Monthly Hours").Range("A1")




Search For Multiple Values In A Column And Display All Results That Are Found - Excel

View Content
Hi,

I'm looking to make a macro that will search column A for a list of values and will return a Msgbox that lists any values that are found. If no values are found I want to show a Msgbox that no values were found.

Example:

Column A generally has 300-500 rows of ticker symbols (e.g. - CNSU , GOOG , APPL , MSFT , NFLX, MET , LXCC , BFN)

I have a list of symbols and I want to see if any of them are in Column A (VNF , MBLF, PNP , LXCC , DRAQ)


Important to note is that the list I am checking may also be up to hundreds of symbols, so I need a solution that is as efficient as possible.


I've found code elsewhere on the internet that does essentially what I want but can only search for 1 symbol. I won't post it as it's probably simpler to start from scratch.


Also, I realize that keeping an excel file list of the tickers I am checking for and then using VLOOKUP would probably be easier, however, I may not have that option at this point.

Thanks!

Pivot Table Help Needed - Applying Autofilter Doesn't Display Columns With Zero Values - Excel

View Content
Hi,

I have a query with Pivot Tables. When I apply an autofilter on a Pivot Table it doesn't show the columns with all zero values, how do I get the table to show these columns?

My motivation for showing these columns with zero values is that I need to calculate some metrics (eg. top left cell of one pivot table divided by top left cell of another pivot table, in %) and so need the pivot tables to be of the same dimension.

Any help on this matter would be much appreciated.

Cheers

Copying The Results Of Autofilter On Data When There Is Only One Result - Excel

View Content
Hey All,

I have a macro that creates sheets and copies the relevant results from a Data sheet that is autofiltered based on three criteria. The problem I am having is when there is only one result from the filter it copies the line below it and I have a feeling it is due to the offset or row count? I should mention that it works perfectly for 2 or more results. Here is the snippet of code that is causing my headaches!

VB:

 'Begins Title filter and selection
FilterRange.AutoFilter Field:=1, Criteria1:="" & cboRepMonth.Value & "" 
 
FilterRange.AutoFilter Field:=2, Criteria1:="" & LoopStudio.Value & "" 
 
FilterRange.AutoFilter Field:=4, Criteria1:="" & cboRepYear.Value & "" 
 
ActiveWorkbook.Sheets("Data").Activate 
 
 'Copies visible entries to the Studio Tab
If ActiveSheet.FilterMode = True Then 
    ActiveSheet.Range("O1").Select 
    Do 
        ActiveCell.Offset(1, 0).Select 
    Loop Until ActiveCell.Rows.Hidden = False 
End If 
If ActiveCell.Offset(1, 0).Rows.Hidden = True Then 
    ActiveCell.Offset(1, 0).Select 
    Selection.Copy Destination:=ActiveWorkbook.Sheets(Studio).Range("B" & LastRow + 3) 
    ActiveWorkbook.Sheets(Studio).Range("B" & LastRow + 2).Formula = "Titles" 
    ActiveWorkbook.Sheets(Studio).Range("B" & LastRow + 2).Font.Underline = xlUnderlineStyleSingle 
Else 
    Range(Selection, Selection.End(xlDown)).Select 
    Selection.Copy Destination:=ActiveWorkbook.Sheets(Studio).Range("B" & LastRow + 3) 
    ActiveWorkbook.Sheets(Studio).Range("B" & LastRow + 2).Formula = "Titles" 
    ActiveWorkbook.Sheets(Studio).Range("B" & LastRow + 2).Font.Underline = xlUnderlineStyleSingle 
End If 


If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines



The data is autofiltered based on the criteria input from a user form. Any assistance would be greatly appreciated. Thanks!

Automatically Filter Unique Values, Lookup Results, Match By Week & Ytd - Excel

View Content
I'm trying to create a report to display the stats for a small team. If possible I'd like it to be completely automated- just generate the source data, open the calc spreadsheet, and hit update. Here's what I have to work with:

spreadsheet1
Column A has 6 digit account numbers in text format
Column M has dates the team member contacted the account
Column AE lists results A, B, C, or D
I output the above spreadsheet once per week for each team member

spreadsheet2
Column A has 6 digit account numbers in text format
Column B has order dates

Here's what I want to be able to report for each member of the team:
1 - how many accounts were contacted for the given week
2-5 - how many of each result the team member got, A, B, C, and D
6 - of the accounts contacted that week, how many of them placed an order that week
7 - how many orders have been placed by all contacted accounts project-to-date, not just that week
8 - how many of the accounts that have been contacted have since placed an order on 3+ separate weeks

Once an account qualifies for #13, they are considered 'complete' and no longer need to be contacted.

I can do it now but it's very clunky. The biggest roadblock to the way I wanted to do it is that while the account numbers are pre-existing, I don't know which team member is contacting what account numbers until they contact them, so I have to create/update the list of accounts for each team member every week. My current process is to run the first report, generate spreadsheet1, filter unique/copy/paste the account numbers to get a contacted list for each team member, run the second report with those numbers, output spreadsheet2, open the calc sheet to autogenerate the weekly stats, then copy/paste them into another tracker sheet. Then I do all that over again to get the project-to-date numbers. I only recently realized that team members could artificially inflate their numbers for point 6 by doing a followup call to an account that's already 'complete' and is going to order anyway, so I'm not sure how I can filter those out without a bunch of extra steps each week.

Any ideas on how to do all this automagically? I can output the first spreadsheet for the whole team and pick out each one's activity or do separate outputs for each team member. Same with the second report, once I have the new/updated list of account numbers for each team member from the first report.

idk
Random Tutorials
Net Present Value / Discounted Cash Flow Calculations
(Intermediate)
Remove #N/A Error Result from Empty VLOOKUP() Formulas
(Intermediate)
IF Statement Introduction & Using Nested IF's
(Easy)
HLOOKUP() Function - Introduction
(Intermediate)
Function and Formulas Lookup in Excel
(Easy)
AND(), OR(), and IF() Statements/Formulas
(Intermediate)
Submit Inquiry Here
  • Prices From $10
Name:*
E-mail:*
Request:*
The macro(s) on this page will be sent with the request.
Contact | Privacy Policy | Disclaimer
Copyright© 2012 TeachExcel.com