Excel

Multi Filtered search and create

Ease of Use

Easy

Version tested with

2000 

Submitted by:

gibbo1715

Description:

This code will search a sheets data, and then create a new sheet for each criteria you search, (upto 10 at a time) 

Discussion:

If you have a sheet that you need to search, but you only want to search one column at a time then this routine will find each instance of what you are searching for and copy each row to another sheet, you can split your seach into ten different criteria each search, the data will be split into a different sheet for each criteria 

Code:

instructions for use

			

'On a Standard Module Option Explicit Sub formshow() 'Show Search Form UserForm1.Show End Sub Function FilterAndCopy(rng As Range, Choice As String, Field As String) Dim FiltRng As Range Worksheets(Choice).Cells.ClearContents rng.AutoFilter Field:=Field, Criteria1:=Choice On Error Resume Next Set FiltRng = rng.SpecialCells(xlCellTypeVisible).EntireRow On Error GoTo 0 FiltRng.Copy Worksheets(Choice).Range("A1") Set FiltRng = Nothing End Function Function CreateSheet(Choice As String) Dim NewSheet As Worksheet On Error GoTo Err: Worksheets(Choice).Select Exit Function Err: Set NewSheet = Worksheets.Add On Error Resume Next NewSheet.Name = Choice On Error GoTo 0 End Function 'In the userform******************************************************* Option Explicit Private Sub CommandButton1_Click() Dim rng As Range Dim ctrl As MSForms.Control Dim Field As String Field = ComboBox1.ListIndex + 1 'Set Error Handling On Error GoTo ws_exit: Application.EnableEvents = False 'Set Range Set rng = ActiveSheet.UsedRange For Each ctrl In UserForm1.Controls If Left(ctrl.Name, 4) = "Text" Then If ctrl.Value <> "" Then CreateSheet ctrl.Value FilterAndCopy rng, ctrl.Value, Field rng.AutoFilter End If End If Next Unload Me Exit Sub ws_exit: Set rng = Nothing Application.EnableEvents = True Unload Me End Sub Private Sub CommandButton2_Click() 'Cancel Button Unload Me End Sub Private Sub UserForm_Initialize() Dim FillRange As Range Dim Cel As Range Dim iLastRow As Long Dim iLastColumn As Long 'Find Last Row iLastRow = 1 'Find Last Column iLastColumn = Cells(1, Columns.Count).End(xlToLeft).Column 'Set Range from A1 to Last Row/Column Set FillRange = Range("A1", Cells(iLastRow, iLastColumn)) For Each Cel In FillRange Me.ComboBox1.AddItem Cel.Text Next ComboBox1.ListIndex = 0 Set Cel = Nothing Set FillRange = Nothing End Sub

How to use:

  1. Open Microsoft Excel
  2. Press Alt + F11 to open the Visual Basic Editor (VBE)
  3. Add a new standard module (Top Left)
  4. Copy the In a Standard Module code above into the right pain
  5. Paste code into the right pane
  6. Add a new userform (Top Left)
  7. add two button and 10 text boxs and a combobox to your userform
  8. double click anywhere on the userform
  9. Copy the userform code above into the right pain
  10. Return to excel and add a button
  11. Attach Macro formshow to button
  12. Thats it, it will now search the sheet with the button on and return the results to seperate worksheets
  13. Note: you can also call the macro from Tools menu - Macro - Macros and call formshow macro
  14. This will search the active sheet so you can drill down your data as much as you like
 

Test the code:

  1. Enter data onto sheet with button on
  2. Click Button ( or use toolbars - Tools Menu - Macro - Macros and call formshow
  3. Userform will now be displayed
  4. Enter Column to search in Dropdown box
  5. Enter search criterias in textboxs
  6. HIt Search Button
  7. for each search criteria you put into a textbox you will get a different worksheet displaying the data****Please Note illegal characters such as /?* etc will not cause this code to crash due to the error handling but will result in a blank worksheet being created - No data will be filtered or copied
 

Sample File:

Userform Filtered search.zip 23.25KB 

Approved by mdmackillop


This entry has been viewed 736 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express