• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Copy Autofilter to another workbook append to last row always

fruitymelon

New Member
The code works fine, but the output not copying the row correctly. It'll always copy the first row followed by filter data.
Further, when i run the code 2 times, it doesn't copy to the last row. It only work on first run.

In addition to above,

1. if Sheet1 of Export1.xlsm is blank copy the data including the header.
Otherwise copy paste from row A2 of ActiveWorksheet (exclude header) into Export1.xlsm (Destination) at the last used row according to header name.

2. If rows exceeded maximum 1,048,576 rows then copy to another new workbook.

Anyone can rectify the above first.. The additional part will appreciate if you have any inputs.

Code:
Sub Copy_AutoFilter()
'Note: This macro use the function LastRow
    Dim My_Range As Range
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim FilterCriteria As String
    Dim CCount As Long
    Dim WSNew As Worksheet, FilterRow As Variant
    Dim wbk1 As Workbook
    Dim wsh1 As Worksheet
    Dim sheetName As String
    Dim rng As Range
    Dim strCom As String
   
    Dim wbDes As Workbook, wkSh As Worksheet
    Dim lCopyLastRow, lDestLastRow As Long
   
    'contains SGA
    strCom = "SGA"

    Set wbk1 = ThisWorkbook
    Set wsh1 = wbk1.Worksheets("Sheet1") ' change sheet name as needed

    Set My_Range = Range("A1:CS" & LastRow(wsh1))
    My_Range.Parent.Select
   
    'Firstly, remove the AutoFilter
    My_Range.Parent.AutoFilterMode = False
   
    FilterRow = Rows("1:1").Find(What:="CORP_Name", Lookat:=xlWhole).Column
   
    My_Range.AutoFilter Field:=FilterRow, Criteria1:="=*" & strCom & "*"
           
    If ActiveWorkbook.ProtectStructure = True Or _
       My_Range.Parent.ProtectContents = True Then
        MsgBox "Sorry, not working when the workbook or worksheet is protected", _
               vbOKOnly, "Copy to new worksheet"
        Exit Sub
    End If

    'Change ScreenUpdating, Calculation, EnableEvents, ....
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    wsh1.DisplayPageBreaks = False

    'Check if there are not more then 8192 areas(limit of areas that Excel can copy)
    CCount = 0
    On Error Resume Next
    CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
    On Error GoTo 0
    If CCount = 0 Then
        MsgBox "There are more than 8192 areas:" _
             & vbNewLine & "It is not possible to copy the visible data." _
             & vbNewLine & "Tip: Sort your data before you use this macro.", _
               vbOKOnly, "Copy to worksheet"
    Else
   
            Set wbDes = Workbooks.Open(Filename:="C:\Users\Wenna\Downloads\excel\Export1.xlsm")
            Set wkSh = Sheets("Sheet1")
            'lDestLastRow = wkSh.Cells(wkSh.Rows.Count, "A").End(xlUp).Offset(1).Row
            lDestLastRow = wkSh.Cells(wkSh.Rows.Count, "A").End(xlUp).Row
       
            My_Range.Parent.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy
            'With wkSh.Range("A1")
            With wkSh.Range("A" & lDestLastRow)
            'With wkSh.Range("A1" & LastRow(wkSh))

                .PasteSpecial Paste:=8
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
                Application.CutCopyMode = False
                .Select
            End With

'         'If you want to delete the rows that you copy, also use this
'         With My_Range.Parent.AutoFilter.Range
'             On Error Resume Next
'             Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
'                       .SpecialCells(xlCellTypeVisible)
'             On Error GoTo 0
'             If Not rng Is Nothing Then rng.EntireRow.Delete
'         End With


    End If

    'Close AutoFilter
    My_Range.Parent.AutoFilterMode = False

    'Restore ScreenUpdating, Calculation, EnableEvents, ....
    'My_Range.Parent.Select
    ActiveWindow.View = ViewMode
    If Not wkSh Is Nothing Then wkSh.Select
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With

End Sub

Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlValues, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function
 
Top