|
Filter Data to Display the Results that Begin With Specified Text or Words in Excel - AutoFilter
This Excel macro automatically filters a set of data based on the words or text that are contained in the beginning of the cell. This uses the autofilter method in Excel.
To use this macro, simply replace the text "Enter Criteria Here" with the text that you would like to filter your data by. Make sure to include the "*" symbol at the end of the text because this is what tells Excel that you want to search for text or words that are in the beginning of a cell in Excel.
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 to Display the Results that Begin With Specified Text or Words in Excel - AutoFilter
Sub AutoFilter_Begins_With()
'Put the * mark after the word in which you would like to filter for this macro
Range("A1").AutoFilter Field:=1, Criteria1:="Enter Criteria Here*"
End Sub
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- 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
- 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.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- 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.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- 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.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- 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.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- 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.
- 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.
- 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.
- 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.
- Go to Step 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.
- You are now ready to run the macro.
Similar Helpful Excel Resources
I have information in a column that follows the order of "### ABCDEFG ##". There are 7,000+ rows of this info. and I need to filter this by a set of the beginning three numbers, there are multiple entries of the beginning three numbers. I've tried doing a text filter with the 'begin with' option but that only works for one number at a time... I have a set of numbers that need to be shown. I know this has to be easy but I just can't find any instructions to do this. Thanks for the help!
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
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
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!
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
I have a long list of item descriptions that I need to filter down to descriptions containing words like "valve" or "ring" or "root" or "epic" (17 key words in all). The basic custom filter looks like it only lets me filter for 2 words.
I have some experience working with advanced filter for things like >100 but that's about it. Any help with this would be so appreciated.
Hi,
I am struggling to find the correct operator in the advanced filter that will filter my database if a particular cell contains data that does not begin with the number combination 1424
Any help is greatly appreciated
Cheers,
Roland
Hi everyone,
Need help here. I have a list in my spreadsheet that contains values beginning with numeric values followed by a alpha character and vice versa. How do I filter those that begin with number only, then those that begin with letters? I was using filter custom function Begins with but dont know what to do next? what should i partner with the begins with function? thanks
Hello all,
I have played around with many variations and this what I have come up with so far. The below code as you can see is a function and I have it in a module called JulianDateConvert since I have not been able to combine it with the Sub (probably cant combine them).
Code:
Public Function CDate2Julian(ReqDate As Date) As String
CDate2Julian = Format(ReqDate - DateSerial(Year(ReqDate) - 1, 12, 31), "000")
End Function
The below code should call the function 'CDate2Julian' above, retrieve the user entered date from the text box called ReqDate and place the converted date into the JulianDate text box.
Code:
Private Sub JulianDate_AfterUpdate()
'RequestForm is the name of the form, JulianDate is the name of the cell to display the julian date, 'and ReqDate is the date entered on the form by the user
Forms!RequestForm!JulianDate = CDate2Julian(Year(Forms!RequestForm!ReqDate)) Mod 10 & CDate2Julian(Forms!RequestForm!ReqDate)
End Sub
Testing
Code:
Print Year(DATE()) Mod 100 & CDate2Julian(DATE())
in immediate mode returns the correct date for 8-11-2009 which is 9223.
Using
Code:
Forms!RequestForm!JulianDate = CDate2Julian(Year(Forms!RequestForm!ReqDate)) Mod 10 & CDate2Julian(Forms!RequestForm!ReqDate)
in immediate mode for the same date above displays 2223 in immediate mode in which the first digit should be a 9.
Can someone please assist me in getting the correct julian date to display on the form either after the date is entered on the form or when a button is pressed.
Thanks,
Andrew
|
|