How to copy only the visible line of Autofilter and paste only values

priisha

New Member
Joined
Apr 4, 2022
Messages
22
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I've been trying to work with my excel data where I need to copy data from one sheet to another sheet.
In the source sheet I have the following data
Doc nr (A)Date (B)Name (C)Nr (D)Cost (E)Amount TCY (E)Currency (F)Description (G)Amount LCY (H)Account2 (I)
564654631100
488967
45631100

I want to copy the data from the source sheet only for the line where the Cost (E) is not equal to 31100 (meaning in this example only the row 1 and 3 should be copied). Also the columns are not copied in the same order but to different order in the target sheet. I have worked out a vba code that works however it also copy the formulas in the source cell which I dont want as I only want it to copy values

VBA Code:
Sub createBFO

Dim ws1 As Worksheet, ws2 As Worksheet, lRow As Long
Set ws1 = ThisWorkbook.Sheets("KGH Ing Moms underlag")
Set ws2 = ThisWorkbook.Sheets("BFO ing moms")

'remove data
Worksheets("BFO ing moms").Range("A2:O200").ClearContents

lRow = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row

With ws1
    .Range("E1").AutoFilter Field:=1, Criteria1:="<>31100" 'add filter to exclude 31100
    .Range("B2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws2.Range("A2")
    .Range("E2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws2.Range("D2")
    .Range("G2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws2.Range("H2")
    .Range("H2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws2.Range("G2")
    .Range("I2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws2.Range("F2") 
    
    .Range("A1").AutoFilter
    
End With

End Sub

I've tried to change the code so the with ws 1 part become
VBA Code:
With ws1
    .Range("E1").AutoFilter Field:=1, Criteria1:="<>31100" 'add filter to exclude 31100
    .Range("B2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy ws2.Range("A2")
    .Range("E2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy ws2.Range("D2")
    .Range("G2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy ws2.Range("H2")
    .Range("H2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy ws2.Range("G2")
    .Range("I2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy ws2.Range("F2").PasteSpecial xlPasteValues


But I got an error saying that code is incorrect.

Please help me!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
just want to add that the error is

Compile error:
Expected: end of statement
 
Upvote 0
The pastespecial need to go on a separate line like
VBA Code:
.Range("I2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy 
ws2.Range("F2").PasteSpecial xlPasteValues
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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