+ Reply to Thread
Results 1 to 11 of 11

Macro which will filter and paste data to new worksheet

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Macro which will filter and paste data to new worksheet

    Hi,

    I need some help with a VBA code as I have no experience with writing a macro.

    I have an unsorted data sheet which I need to extract information from and with each data extract I need it to create and save a new workbook.
    1. On the tab GL – Column A I have a branch code consisting of 3 digits. What I need is a macro which creates a new workbook splitting out each branch.

    e.g. in the example supplied I would like 5 new worksheets created showing data for branch 181 on one work sheet, 182 on another, 183 on another etc etc

    2. For each new worksheet am I able to define the worksheet name – ideally I would like the branch name e.g. “181”

    3. Is there any way in which I can define the workbook name and location to save to (if it doesn’t exist I would like it created).

    I know this is a big ask – if there is a similar workbook that exists that I can copy the logic that you know of please point me in the right direction.

    Any help is appreciated.

    Paul
    Attached Files Attached Files

  2. #2
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Macro which will filter and paste data to new worksheet

    Hello Paul

    Give a try to this.
    Sub PagesByDescription()
    Dim rRange As Range, rCell As Range
    Dim wSheet As Worksheet
    Dim wSheetStart As Worksheet
    Dim strText As String
    
        Set wSheetStart = ActiveSheet
        wSheetStart.AutoFilterMode = False
        'Set a range variable to the correct item column
        Set rRange = Range("A1", Range("A65536").End(xlUp))
        
            'Delete any sheet called "UniqueList"
            'Turn off run time errors & delete alert
            On Error Resume Next
            Application.DisplayAlerts = False
            Worksheets("UniqueList").Delete
            
            'Add a sheet called "UniqueList"
            Worksheets.Add().Name = "UniqueList"
            
               'Filter the Set range so only a unique list is created
                With Worksheets("UniqueList")
                    rRange.AdvancedFilter xlFilterCopy, , _
                     Worksheets("UniqueList").Range("A1"), True
                     
                     'Set a range variable to the unique list, less the heading.
                     Set rRange = .Range("A2", .Range("A65536").End(xlUp))
                End With
                
                On Error Resume Next
                With wSheetStart
                    For Each rCell In rRange
                      strText = rCell
                     .Range("A1").AutoFilter 1, strText
                        Worksheets(strText).Delete
                        'Add a sheet named as content of rCell
                        Worksheets.Add().Name = strText
                        'Copy the visible filtered range _
                        (default of Copy Method) and leave hidden rows
                        .UsedRange.Copy Destination:=ActiveSheet.Range("A1")
                        ActiveSheet.Cells.Columns.AutoFit
                    Next rCell
                End With
                
            With wSheetStart
                .AutoFilterMode = False
                .Activate
            End With
            
            On Error GoTo 0
            Application.DisplayAlerts = True
    End Sub
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  3. #3
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Re: Macro which will filter and paste data to new worksheet

    Hi

    Thank you Fotis for replying so quickly.

    That is what I am trying to achieve but rather than create a new worksheet I would like it to create a new workbook - can the code be tweeked to do this?

    Ideally I would like the files to be saved directly to here:
    P:\Test\Finance\manacc\MAN_ACC\Test2\2014\Mth0214\Transactional Reports\Hygiene

    If that path doesnt already exist then I would like it created.

    Could the file names be automatically defined e.g. "Transactional Report and the branch number (from column A) and the Month (I can define this in the macro each month)

  4. #4
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Macro which will filter and paste data to new worksheet

    HaHoBe, in this thread provided a code for that! I don't like to copy that code and gives to you as mine!

  5. #5
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Re: Macro which will filter and paste data to new worksheet

    Hi,

    Thanks for the link, I still need a bit of help please:

    Currently the macro saves the file to the same desitnation as the orignal file:

    ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & strText & Format(Now, "YYMMDD_HHNNSS") & ".xlsm", FileFormat:=52
    Is there any way I can change this to:

    P:\Test\Finance\manacc\MAN_ACC\Test2\2014\Mth0214\Transactional Reports\Hygiene

    And then rather than that file name e.g. "182140318_090259" could I have:

    "Transactional Report and the branch number (from column A or the tab name) and the Month (I can define this in the macro each month)

    I dont know how to amend the code to do this.

    Regards

    Paul

  6. #6
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Macro which will filter and paste data to new worksheet

    ...I dont know how to amend the code to do this.

    Me too Paul!! Sorry!

  7. #7
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Re: Macro which will filter and paste data to new worksheet

    Hi,

    I have found a code here:

    https://sites.google.com/a/madrocket.../sheet1-to-wbs

    But I would like to try and add elements of this code to yours Fotis.

    Is there any way I can add these elements to your original code?:

    'Path to save files into, remember the final \
        SvPath = "P:\Initial Washroom Solutions\Finance\manacc\MAN_ACC\RENTOKIL\2014\Mth0314\Transactional Reports\Hygiene\"
    ActiveWorkbook.SaveAs SvPath & MyArr(Itm) & " Transactional Report March14", xlNormal
    I have uploaded a file which contains the 3 macros - I would like to merge the 3 really (your orignal code with the file path and file name elements).

    Regards

    Paul
    Attached Files Attached Files
    Last edited by JBeaucaire; 12-27-2019 at 04:02 AM.

  8. #8
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Macro which will filter and paste data to new worksheet

    I just asked for help Paul. Lt's wait for a while.

  9. #9
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro which will filter and paste data to new worksheet

    Try:
    Sub macro_1()
    Dim count, ws1
    Application.ScreenUpdating = False
    Set ws1 = Activesheet
    For count = 2 To ws1.Range("A" & Rows.count).End(xlUp).Row
        On Error GoTo not_found
            ws1.Rows(count).Copy Sheets("" & ws1.Range("A" & count)).Range("A" & Rows.count).End(xlUp).Offset(1, 0)
        On Error GoTo 0
    Next count
    ThisWorkbook.SaveAs "P:\Test\Finance\manacc\MAN_ACC\Test2\2014\Mth0214\Transactional Reports\Hygiene\" & ws1.name & ".xls"
    Application.ScreenUpdating = True
    Exit Sub
    not_found:
    Sheets.Add
    ActiveSheet.Name = ws1.Range("A" & count)
    Resume
    End Sub
    Last edited by ragulduy; 03-18-2014 at 06:30 AM.

  10. #10
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Macro which will filter and paste data to new worksheet

    Perhaps:
    Const csSAVE_PATH                  As String = "P:\Initial Washroom Solutions\Finance\manacc\MAN_ACC\RENTOKIL\2014\Mth0314\Transactional Reports\Hygiene\"
    ' you may change this month
    Const csMONTH_NAME                 As String = "March14"
    Sub PagesByDescription()
        Dim rRange As Range, rCell     As Range
        Dim wSheet                     As Worksheet
        Dim wSheetStart                As Worksheet
        Dim wPaste                     As Worksheet
        Dim strText                    As String
    
        Set wSheetStart = ActiveSheet
        wSheetStart.AutoFilterMode = False
        'Set a range variable to the correct item column
        Set rRange = Range("A1", Range("A65536").End(xlUp))
    
        'Delete any sheet called "UniqueList"
        'Turn off run time errors & delete alert
        On Error Resume Next
        Application.DisplayAlerts = False
        Worksheets("UniqueList").Delete
    
        'Add a sheet called "UniqueList"
        Worksheets.Add().Name = "UniqueList"
    
        'Filter the Set range so only a unique list is created
        With Worksheets("UniqueList")
            rRange.AdvancedFilter xlFilterCopy, , _
                                  Worksheets("UniqueList").Range("A1"), True
    
            'Set a range variable to the unique list, less the heading.
            Set rRange = .Range("A2", .Range("A65536").End(xlUp))
        End With
    
        On Error Resume Next
        With wSheetStart
            For Each rCell In rRange
                strText = rCell
                .Range("A1").AutoFilter 1, strText
                Worksheets(strText).Delete
                'Add new workbook with sheet named as content of rCell
                Set wPaste = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
                wPaste.Name = strText
                'Copy the visible filtered range _
                 (default of Copy Method) and leave hidden rows
                .UsedRange.Copy Destination:=wPaste.Range("A1")
                wPaste.Cells.Columns.AutoFit
                With wPaste.Parent
                    .SaveAs csSAVE_PATH & strText & " Transactional Report " & csMONTH_NAME, xlNormal
                    .Close savechanges:=False
                End With
            Next rCell
        End With
    
        With wSheetStart
            .AutoFilterMode = False
            .Activate
        End With
    
        On Error GoTo 0
        Application.DisplayAlerts = True
    End Sub
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  11. #11
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Re: Macro which will filter and paste data to new worksheet

    Hi,

    Thanks for the codes supplied here.

    @ragulduy - I dont think this worked as it has not created new workbooks.

    @Izandol - I think this may just be what I am after!! I will test it and report back (currently out of the office)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need help with a macro to filter data and paste it in the corresponding worksheet
    By odoualex in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-17-2013, 02:35 PM
  2. Macro to Copy/Paste Cells if Data is found with Filter
    By CMPidgeon in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-16-2012, 04:05 PM
  3. [SOLVED] Macro to Filter Data and Paste in new Workbooks
    By willy91785 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-19-2010, 11:14 AM
  4. how to filter and copy data from one sheet and paste to new worksheet
    By mr_asrul in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-03-2008, 07:15 AM
  5. Replies: 5
    Last Post: 03-18-2006, 08:45 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1