<<Convert Excel Spreadsheets to Web Pages | Trading Software That Operates Within Excel | Convert Excel, Access & Other Databases | Merge Excel Files>>
Ozgrid, Experts in Microsoft Excel Spreadsheets

Find 3 Matching Criteria

TRY OUT: Smart-VBA | Code-VBA | Analyzer-XL | Downloader-XL | Trader-XL| More Free Downloads.. Best Value: Finance Templates Bundle

Excel: Find Feature to Find 3 Matching Criteria in 3 Columns in Excel

Got any Excel Questions? Free Excel Help .

Lot's More: Excel VBA See Also:Advanced Excel Find add-in

Advanced Excel Find Download Working Example

The standard Excel Find feature is great for locating matching cells. However, it cannot be use 'as is' to locate, say 3, matching cells on the same row within a table. What we will do, to make it different to the normal find, is allow the user to specify more than one item to locate. For example, if you have a large table of data (say A1:H1000), you may wish to find a specific row in that table where 3, or more items (if modified), occur on the one row. The number of items can be greater or smaller than 3, but for this example we will use 3.

To create this custom find you will need.

A) 1 UserForm.

B) 3 ComboBoxes. Named ComboBox1, ComboBox2 and ComboBox3. Place these vertically on the left side of UserForm with ComboBox1 at the top and ComboBox3 and the bottom. Set the Enabled Property of ComboBox2 and ComboBox3 to False.

C) 5 Labels. Named Label1 (positioned above Combobox1), Label2 (positioned above Combobox2), Label3 (positioned above Combobox3), Label4 positioned above Label1 and Label 4 anywhere for now. Change the Caption Property of Label4 to read "Select up to 3 fields". Change the Caption Property of Label5 to read "Matching Rows. Double click to go there".

D) 2 CommandButtons. CommandButton1 and CommandButton2. Change the Caption Property of CommandButton1 to "Find" and the Caption Property of CommandButton2 to "Close". Postion both of these to the top right of the UserForm.

E) 1 ListBox. Named ListBox1. Postion this below the 2 CommandButtons and place label5 above it. Make ListBox1 the same width as Label5.

That is all we need for creating the form now it's time to add the all important code. Double click the UserForm and add all the code shown below;

Option Explicit'Module Level VariablesDim rRange As RangeDim strFind1 As StringDim strFind2 As StringDim strFind3 As StringPrivate Sub ComboBox1_Change()'Pass chosen value to String variable strFind1strFind1 = ComboBox1'Enable ComboBox2 only if value is chosenComboBox2.Enabled = Not strFind1 = vbNullStringEnd SubPrivate Sub ComboBox2_Change()'Pass chosen value to String variable strFind1strFind2 = ComboBox2'Enable ComboBox3 only if value is chosenComboBox3.Enabled = Not strFind2 = vbNullStringEnd SubPrivate Sub ComboBox3_Change()'Pass chosen value to String variable strFind1strFind3 = ComboBox3End SubPrivate Sub CommandButton1_Click()'Procedure level variablesDim lCount As LongDim lOccur As LongDim rCell As RangeDim rCell2 As RangeDim rCell3 As RangeDim bFound As Boolean'At least one value, from ComboBox1 must be chosenIf strFind1 & strFind2 & strFind3 = vbNullString Then        MsgBox "No items to find chosen", vbCritical            Exit Sub 'Go no furtherElseIf strFind1 = vbNullString Then        MsgBox "A value from " & Label1.Caption _            & " must be chosen", vbCritical        Exit Sub 'Go no further     End If'Clear any old entriesOn Error Resume NextListBox1.ClearOn Error GoTo 0'If String variable are empty pass the wildcard characterIf strFind2 = vbNullString Then strFind2 = "*"If strFind3 = vbNullString Then strFind3 = "*"'Set range variable to first cell in table.Set rCell = rRange.Cells(1, 1)'Pass the number of times strFind1 occurslOccur = WorksheetFunction.CountIf(rRange.Columns(1), strFind1)'Loop only as many times as strFind1 occursFor lCount = 1 To lOccur'Set the range variable to the found cell. This is then also _         used to Start the next Find from (After:=rCell)Set rCell = rRange.Columns(1).Find(What:=strFind1, After:=rCell, _                LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _                SearchDirection:=xlNext, MatchCase:=False)'Check each find to see if strFind2 and strFind3 occur _         on the same row.If rCell(1, 2) Like strFind2 And rCell(1, 3) Like strFind3 Then            bFound = True 'Used to not show message box for no value found.'Add the address of the found cell and the cell on the _             same row but 2 columns to the right.ListBox1.AddItem rCell.Address & ":" & rCell(1, 3).Address         End If    Next lCountIf bFound = False Then 'No matchMsgBox "Sorry, no matches", vbOKOnlyEnd IfEnd SubPrivate Sub CommandButton2_Click()'Close UserFormUnload MeEnd SubPrivate Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)'Check for range addressesIf ListBox1.ListCount = 0 Then Exit Sub'GoTo doubled clicked addressApplication.Goto Range(ListBox1.Text), TrueEnd SubPrivate Sub UserForm_Initialize()'Procedure level moduleDim lRows As Long'Set Module level range variable to CurrentRegion _of the SelectionSet rRange = Selection.CurrentRegion    If rRange.Rows.Count < 2 Then ' Only 1 row        MsgBox "Please select any cell in your table first", vbCritical        Unload Me 'Close Userform        Exit Sub    Else        With rRange'Set Label Captions to the Table headingsLabel1.Caption = .Cells(1, 1)             Label2.Caption = .Cells(1, 2)             Label3.Caption = .Cells(1, 3)'Set RowSource of ComboBoxes to the appropriate columns _             inside the tableComboBox1.RowSource = .Columns(1).Offset(1, 0).Address             ComboBox2.RowSource = .Columns(2).Offset(1, 0).Address             ComboBox3.RowSource = .Columns(3).Offset(1, 0).Address        End With    End IfEnd SubPrivate Sub UserForm_Terminate()'Destroy Module level variablesSet rRange = NothingstrFind1 = vbNullStringstrFind2 = vbNullStringstrFind3 = vbNullStringEnd Sub

Now go to Insert>Module and add this code

Sub ShowForm()On Error Resume NextUserForm1.ShowOn Error GoTo 0End Sub

Download Working Example

Excel Dashboard Reports & Excel Dashboard Charts 50% Off Become an ExcelUser Affiliate & Earn Money

Special! Free Choice of Complete Excel Training Course OR Excel Add-ins Collection on all purchases totaling over $64.00. ALLpurchases totaling over $150.00 gets you BOTH! Purchases MUST be made via this site. Send payment proof to [email protected] 31 days after purchase date.



Instant Download and Money Back Guarantee on Most Software

Try out:Analyzer XL |Downloader XL |Smart VBA |Trader XL Pro (best value) |ConsoXL | MergeXL | O2OLAP for Excel | MORE>>

Excel Trader PackageTechnical Analysis in Excel With $139.00 of FREE software!

Microsoft � and Microsoft Excel � are registered trademarks of Microsoft Corporation. OzGrid is in no way associated with Microsoft

Some of our more popular products are below...
Convert Excel Spreadsheets To Webpages | Trading In Excel | Construction Estimators | Finance Templates & Add-ins Bundle | Code-VBA | Smart-VBA | Print-VBA | Excel Data Manipulation & Analysis | Convert MS Office Applications To...... | Analyzer Excel | Downloader Excel | MSSQL Migration Toolkit | Monte Carlo Add-in | Excel Costing Templates