|
|
|
|
|
|
Excel
|
Filter a sheet and copy results to another sheet
|
|
Ease of Use
|
Easy
|
Version tested with
|
xl2000, 2003
|
Submitted by:
|
gibbo1715
|
Description:
|
The code will ask you what you would like to search for, it will then filter a column based on what you ask for and then copy the results into another sheet, Basically its a copy paste routine
|
Discussion:
|
If you have a sheet that you need to search, but you only want to search one column then this routine will find each instance of what you are searching for and copy each row to another sheet
|
Code:
|
instructions for use
|
Option Explicit
Function FilterAndCopy(rng As Range, Choice As String)
Dim FiltRng As Range
Worksheets("Sheet2").Cells.ClearContents
rng.AutoFilter Field:=1, Criteria1:=Choice
On Error Resume Next
Set FiltRng = rng.SpecialCells(xlCellTypeVisible).EntireRow
On Error GoTo 0
FiltRng.Copy Worksheets("Sheet2").Range("A1")
Worksheets("Sheet2").Select
Range("A1").Select
Set FiltRng = Nothing
End Function
Sub formshow()
UserForm1.Show
End Sub
Option Explicit
Private Sub CommandButton1_Click()
Dim rng As Range
On Error GoTo ws_exit:
Application.EnableEvents = False
Set rng = ActiveSheet.UsedRange
If TextBox1.Value = "" Then GoTo ws_exit:
FilterAndCopy rng, TextBox1.Value
rng.AutoFilter
ws_exit:
Set rng = Nothing
Application.EnableEvents = True
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
|
How to use:
|
- Open Microsoft Excel
- Press Alt + F11 to open the Visual Basic Editor (VBE)
- Add a new standard module (Top Left)
- Copy the In a Standard Module code above into the right pain
- Paste code into the right pane
- Add a new userform (Top Left)
- add two button and a text box to your userform
- double click anywhere on the userform
- Copy the userform code above into the right pain
- Return to excel and add a button
- Attach Macro formshow to button
- Thats it, it will now search the sheet with the button on and return the results
- Results returned to Sheet2 so you must ensure you have a worksheet called Sheet2
|
Test the code:
|
- Enter data onto sheet with button on
- Click Button
- Userform will now be displayed
- Enter search criteria in textbox (Searches Column A)
- HIt Search Button
- Your results will now be displayed
|
Sample File:
|
copy and paste using filter.zip 11.1KB
|
Approved by mdmackillop
|
This entry has been viewed 611 times.
|
|