You are here:

Excel/Find filter and cut paste in a new sheet

Advertisement


Question
Hi tom ! How R U ? Dear tom below is the code you have previously provided me and its working great.

I want a little change as it matches and paste the matched value in a new sheet. Some times it creates a problem if there is a spelling mistake in the data. What i want is to do is when it match the data it will cut the data from source and paste the data in new sheet.

Below is the code...

Sub FilterData()
Dim r As Range, r1 As Range, r2 As Range
Dim bFound As Boolean, s As String
Dim sh As Worksheet, sh1 As Worksheet
Dim v As Variant, cell As Range, i As Long
On Error Resume Next
Set r = Application.InputBox("Select cells with filter values", Type:=8)
On Error GoTo 0
If r Is Nothing Then Exit Sub
v = r.Value
Set sh = Worksheets("Sheet1")
Worksheets.Add After:=Worksheets(Worksheets.Count)
Set sh1 = ActiveSheet
sh.Select
Set r2 = sh.Range("B2", sh.Cells(sh.Rows.Count, "B").End(xlUp))
Set r1 = sh.Rows(1)
For Each cell In r2
 bFound = False
 s = cell.Offset(0, 5).Text & "|" & cell.Offset(0, 6).Text & "|" & cell.Offset(0, 7).Text
 For i = LBound(v, 1) To UBound(v, 1)
   If InStr(1, s, v(i, 1), vbTextCompare) Then
      bFound = True
      Exit For
   End If
 Next
 If bFound Then
      Set r1 = Union(r1, cell.EntireRow)
 End If
Next
r1.Copy sh1.Rows(1)
sh1.Select
sh1.Columns.AutoFit
Application.CutCopyMode = False
'r1.Select
End Sub

Kindly help me.

Thanks & Best Regards
Amit

Answer
Amit,

the below is my guess.

test this on a copy of your workbook


Sub FilterData()
Dim r As Range, r1 As Range, r2 As Range, r1a As Range
Dim bFound As Boolean, s As String
Dim sh As Worksheet, sh1 As Worksheet
Dim v As Variant, cell As Range, i As Long
On Error Resume Next
Set r = Application.InputBox("Select cells with filter values", Type:=8)
On Error GoTo 0
If r Is Nothing Then Exit Sub
v = r.Value
Set sh = Worksheets("Sheet1")
Worksheets.Add After:=Worksheets(Worksheets.Count)
Set sh1 = ActiveSheet
sh.Select
Set r2 = sh.Range("B2", sh.Cells(sh.Rows.Count, "B").End(xlUp))
Set r1a = sh.Rows(1)
For Each cell In r2
 bFound = False
 s = cell.Offset(0, 5).Text & "|" & cell.Offset(0, 6).Text & "|" & cell.Offset(0, 7).Text
 For i = LBound(v, 1) To UBound(v, 1)
   If InStr(1, s, v(i, 1), vbTextCompare) Then
      bFound = True
      Exit For
   End If
 Next
 If bFound Then
   If r1 Is Nothing Then
      Set r1 = cell.EntireRow
   Else
      Set r1 = Union(r1, cell.EntireRow)
   End If
 End If
Next
r1a.Copy sh1.Rows(1)
r1.Copy sh1.Rows(2)
sh1.Select
sh1.Columns.AutoFit
r1.EntireRow.Delete
Application.CutCopyMode = False
End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.