Filter by a name in a column and paste the otput columns in another workbook using VBA

RoseChapman

New Member
Joined
Jun 12, 2018
Messages
40
Hi all, I hope you can help me. I am trying to filter by a name "Tree" in column "E", and add the values in columns of columns "B", "D", "G" in another spreadsheet ("Sheet5"). My code is shown below, which it does the filtering but it pastes Columns A to F and I would like to paste only columns B, D and G. Please see my code below. Your help would be very much appreciated. Many thanks. Rose

Sub Filtering()


Dim Lastrow As Long


With Sheets("DATA_Sheet")

If .Range("E:E").Find("Tree", , xlValues, xlWhole, , , False) Is Nothing Then
MsgBox "No ""Changed"" rows found. ", , "No Rows Copied": Exit Sub
Else

Application.ScreenUpdating = False
.Columns(7).Hidden = True
Lastrow = .Range("G" & Rows.Count).End(xlUp).Row
.Range("E1:E" & Lastrow).AutoFilter Field:=1, Criteria1:="Tree"
.Range("A1:G" & Lastrow).SpecialCells(xlCellTypeVisible).Copy

Sheets("Sheet5").Range("A:G").PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False
.AutoFilterMode = False

'Position on cell A4
With Application
.CutCopyMode = False

.Goto Sheets("Sheet5").Range("A:G")

.ScreenUpdating = True
End With
.Columns(7).Hidden = False
MsgBox "All matching data has been copied.", , "Copy Complete"

End If

End With


End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi & welcome to MrExcel
How about
Code:
Sub Filtering()
   Dim Lastrow As Long
   
   With Sheets("data_Sheet")
   
      If .Range("E:E").Find("Tree", , xlValues, xlWhole, , , False) Is Nothing Then
         MsgBox "No ""Changed"" rows found. ", , "No Rows Copied": Exit Sub
      End If
      
      Application.ScreenUpdating = False
      Lastrow = .Range("G" & Rows.Count).End(xlUp).row
      .Range("A1:G" & Lastrow).AutoFilter Field:=5, Criteria1:="Tree"
      Intersect(.AutoFilter.Range, .Range("B:B,D:D,G:G")).Copy
      
      Sheets("Sheet5").Range("A:G").PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False
      .AutoFilterMode = False
      
      'Position on cell A4
      With Application
         .CutCopyMode = False
         .Goto Sheets("Sheet5").Range("A:G")
         .ScreenUpdating = True
      End With
      MsgBox "All matching data has been copied.", , "Copy Complete"
      
   End With
End Sub
 
Upvote 0
Hello! thank you very much for the welcome, much appreciated!
You've made my day! this works now! you solve my problem!. I don't know how to thank you!. Great stuff! Thank you so much. My best regards to you
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top