Consulting

Results 1 to 3 of 3

Thread: VBA Auto Filter + Paste to New Sheets + Autofill

  1. #1

    VBA Auto Filter + Paste to New Sheets + Autofill

    Dear Experts!!!!


    I have drafted a macro to automate my excel.



    1. Autofilter and create new worksheets based on the filter value with keeping the same format of the original file.
    2. AutoFill Destination Range("A5: A6") to the end of the column.
    3. Loop the process until the filter fields get over.
    4. Filter Range will vary. (Range("C4:AT12").Select)). So macro should find the end of the range automatically.


    Unfortunately, the macro is not copying the range I need to Paste on the newly created sheets.
    My filter Row Range is from
    A6 till AT6. The range I need to copy to new sheets starts from C4 till AT and end of the range.



    Can any one help me to fix this?
    I have attached a sample file with macro.


    Thanks in advance!



    Sub AutoFilterAndCreateNewSheets()
    Application.ScreenUpdating = False
    Dim x As Range
    Dim rng As Range
    Dim last As Long
    Dim sht As String
    
    
    'specify sheet name in which the data is stored
    sht = "Timesheet"
    
    
    'change filter column in the following code
    last = Sheets(sht).Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = Sheets(sht).Range("A6:AT" & last)
    
    
    Sheets(sht).Range("A6:A" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("XFD1"), Unique:=True
    
    
    For Each x In Range([XFD2], Cells(Rows.Count, "XFD").End(xlUp))
    
    
    With rng
    .AutoFilter
    .AutoFilter Field:=1, Criteria1:=x.Value
    .SpecialCells(xlCellTypeVisible).Copy
    
    
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
    ActiveSheet.Paste
    End With
    Next x
    
    
    ' Turn off filter
    Sheets(sht).AutoFilterMode = False
    
    
    With Application
    .CutCopyMode = False
    .ScreenUpdating = True
    End With
    
    
    End Sub
    Attached Files Attached Files

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    .Offset(-2, 2).Resize(rng.Rows.Count + 2, rng.Columns.Count - 2).SpecialCells(xlCellTypeVisible).Copy
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Oh great..!!! you are the real Grand Master. Thanks a million !!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •