I'm using code below to copy rows from Sheet1 to Sheet2. I have 3 questions about.
- Why this function always copy row A2? Even if value is "0".
- How to copy just value, no formatting?
- Is it possible to skip column B when copy? "C" from Sheet1 will be "B" in Sheet2, etc.
Sub COPY_SA() Dim ws1 As Worksheet, ws2 As Worksheet Dim rng As Range, rngToCopy As Range Dim lastrow As Long 'change Sheets to suit Set ws1 = ThisWorkbook.Worksheets("SA") Set ws2 = ThisWorkbook.Worksheets("JC_input") With ws1 'assumung that your data stored in column A:D, Sheet1 lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row Set rng = .Range("A2:D" & lastrow) 'clear all filters .AutoFilterMode = False With rng 'apply filter .AutoFilter Field:=4, Criteria1:=">0" On Error Resume Next 'get only visible rows Set rngToCopy = .SpecialCells(xlCellTypeVisible) On Error GoTo 0 End With 'copy range If Not rngToCopy Is Nothing Then rngToCopy.CopyDestination:=ws2.Range("A2") 'clear all filters .AutoFilterMode = False End With Application.CutCopyMode = False End Sub
I manage to modify like below. Still have issue with range in Worksheet "ws1". Cannot set filter in second row and copy range from row 3. That is why added: "ws2.Rows(3).Delete". Code always copy row 1. Row 1 got some comments. Row 2 got column names.
Sub COPY_SA() Dim ws1 As Worksheet, ws2 As Worksheet Dim rng As Range, rngToCopy As Range Dim lastrow As Long 'change Sheets to suit Set ws1 = ThisWorkbook.Worksheets("SA") Set ws2 = ThisWorkbook.Worksheets("JC_input") With ws1 'assumung that data stored in column C:E, Sheet1 lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row 'can not make range from row 3 ??? Set rng = .Range("C1:E" & lastrow) 'clear all filters .AutoFilterMode = False With rng 'apply filter with criteria in column 3 of range C:E 'can not make filter in row 2 ??? .AutoFilter Field:=3, Criteria1:=">0" On Error Resume Next 'get only visible rows Set rngToCopy = .SpecialCells(xlCellTypeVisible) On Error GoTo 0 End With 'copy range If Not rngToCopy Is Nothing Then rngToCopy.Range("A:C").Copy 'paste from row 3 ws2.Range("A3").PasteSpecial Paste:=xlValues 'delete no needed row ws2.Rows(3).Delete 'clear all filters .AutoFilterMode = False End With Application.CutCopyMode = False If Not ActiveSheet.AutoFilterMode Then ws1.Range("2:2").AutoFilter End If End Sub