+ Reply to Thread
Results 1 to 8 of 8

Macro for auto filter & Copy to new workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    05-10-2011
    Location
    india
    MS-Off Ver
    MS365
    Posts
    28

    Macro for auto filter & Copy to new workbook

    Dear VBA Guru’s,

    I am looking for a VBA Code to smoothen the below process.

    I have a workbook called Dept. queries and it contains two sheets 1. Data (updated on daily basis) 2. Dept ( Contains Dept. Names)

    Based on the sheet 2 (Dept names) Macro needs to autofilter the exact dept name on sheet (Data) and copy. (Note: Dept. names can be added if there is a new dept. added)

    Copy destination would be the workbook I have already created workbook in the same folder with the same dept name.

    I have attached the workbook for your reference.

    Appreciate Immediate help on this !!!!!
    Attached Files Attached Files
    Last edited by learningkid0808; 07-15-2013 at 10:14 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro for auto filter & Copy to new workbook

    Hi..

    This will do it..

    You need to change the file path to suit you.. (change C:\Departments\ to where you have the files on your PC)

    Private Sub CommandButton1_Click()
    Dim Crit1 As String
    Dim LastRow As Long, LastRow2 As Long, i As Long
    Dim myRange
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    LastRow = Sheets("Dept").Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To LastRow
    Crit1 = Sheets("Dept").Cells(i, 1).Value
    Sheets("Data").Range("A1:C" & LastRow).AutoFilter Field:=1, Criteria1:=Crit1
    Application.Workbooks.Open ("C:\Departments\" & Crit1 & ".xlsx")
    LastRow2 = Workbooks(Crit1 & ".xlsx").Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Set myRange = Workbooks("Dept queries.xlsm").Sheets("Data").Range("A2:C" & Workbooks("Dept queries.xlsm").Sheets("Data").Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible)
    
    myRange.Copy Destination:=Workbooks(Crit1 & ".xlsx").Sheets("Sheet1").Cells(LastRow2 + 1, 1)
    Workbooks(Crit1 & ".xlsx").Close SaveChanges:=True
    
    Next i
    If Sheets("Data").AutoFilterMode Then Sheets("Data").AutoFilterMode = False
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "Data Has Been Copied!"
    End Sub
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    05-10-2011
    Location
    india
    MS-Off Ver
    MS365
    Posts
    28

    Re: Macro for auto filter & Copy to new workbook

    Hi.... APO....

    One more help on this code, copy destination ensure that the previous details should be deleted before pasting the new values.

    Thank you for your immediate help....
    Quote Originally Posted by apo View Post
    Hi..

    This will do it..

    You need to change the file path to suit you.. (change C:\Departments\ to where you have the files on your PC)

    Private Sub CommandButton1_Click()
    Dim Crit1 As String
    Dim LastRow As Long, LastRow2 As Long, i As Long
    Dim myRange
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    LastRow = Sheets("Dept").Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To LastRow
    Crit1 = Sheets("Dept").Cells(i, 1).Value
    Sheets("Data").Range("A1:C" & LastRow).AutoFilter Field:=1, Criteria1:=Crit1
    Application.Workbooks.Open ("C:\Departments\" & Crit1 & ".xlsx")
    LastRow2 = Workbooks(Crit1 & ".xlsx").Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Set myRange = Workbooks("Dept queries.xlsm").Sheets("Data").Range("A2:C" & Workbooks("Dept queries.xlsm").Sheets("Data").Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible)
    
    myRange.Copy Destination:=Workbooks(Crit1 & ".xlsx").Sheets("Sheet1").Cells(LastRow2 + 1, 1)
    Workbooks(Crit1 & ".xlsx").Close SaveChanges:=True
    
    Next i
    If Sheets("Data").AutoFilterMode Then Sheets("Data").AutoFilterMode = False
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "Data Has Been Copied!"
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro for auto filter & Copy to new workbook

    Hi..

    One more help on this code, copy destination ensure that the previous details should be deleted before pasting the new values.
    Currently, it appends the data to the next available row..

    If you want to clear the data on each sheet before adding fresh data..

    After this line:
    Application.Workbooks.Open ("C:\Departments\" & Crit1 & ".xlsx")
    Add this..
    Workbooks(Crit1 & ".xlsx").Sheets("Sheet1").UsedRange.ClearContents

  5. #5
    Registered User
    Join Date
    05-10-2011
    Location
    india
    MS-Off Ver
    MS365
    Posts
    28

    Re: Macro for auto filter & Copy to new workbook

    [SOLVED]Thank you every one for your quick help on this.... this is really motivating me to learn things faster

  6. #6
    Registered User
    Join Date
    05-10-2011
    Location
    india
    MS-Off Ver
    MS365
    Posts
    28

    Re: Macro for auto filter & Copy to new workbook

    Hi APO.....
    After using the macro multiple times found that, it would be very helpful if the macro read the path from the cell value on list tab. Cell.value"F4"

  7. #7
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro for auto filter & Copy to new workbook

    Hi..

    How about Browsing for the Folder using a Browse Dialogue...

    Private Sub CommandButton1_Click()
    Dim Crit1 As String, myFolder As String
    Dim LastRow As Long, LastRow2 As Long, i As Long
    Dim myRange
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    LastRow = Sheets("Dept").Range("A" & Rows.Count).End(xlUp).Row
    Continue:
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Show
        If (.SelectedItems.Count = 0) Then Exit Sub
        myFolder = .SelectedItems(1)
    End With
    
    For i = 2 To LastRow
    Crit1 = Sheets("Dept").Cells(i, 1).Value
    Sheets("Data").Range("A1:C" & LastRow).AutoFilter Field:=1, Criteria1:=Crit1
    On Error GoTo ErrHand
    Application.Workbooks.Open (myFolder & "\" & Crit1 & ".xlsx")
    Workbooks(Crit1 & ".xlsx").Sheets("Sheet1").UsedRange.ClearContents
    LastRow2 = Workbooks(Crit1 & ".xlsx").Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Set myRange = Workbooks("Dept queries.xlsm").Sheets("Data").Range("A2:C" & Workbooks("Dept queries.xlsm").Sheets("Data").Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible)
    
    myRange.Copy Destination:=Workbooks(Crit1 & ".xlsx").Sheets("Sheet1").Cells(LastRow2 + 1, 1)
    Workbooks(Crit1 & ".xlsx").Close SaveChanges:=True
    
    Next i
    If Sheets("Data").AutoFilterMode Then Sheets("Data").AutoFilterMode = False
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Sheets("Dept").Cells(4, 6).Value = myFolder
    MsgBox "Data Has Been Copied!"
    
    ErrHand:
        Select Case Err.Number
        Case 1004
            msg = "Error: " & Err.Number & vbCrLf & Err.Description
            MsgBox msg, vbOKOnly, "Incorrect Folder Selected"
            Err.Clear
            GoTo Continue
            Resume Next
        End Select
    End Sub
    Note: It will also store the most recent folder selection in your Dept sheet..
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    05-10-2011
    Location
    india
    MS-Off Ver
    MS365
    Posts
    28

    Re: Macro for auto filter & Copy to new workbook

    Thanks a lot..... APO.... This was really helpful.....

+ 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. Replies: 4
    Last Post: 06-04-2013, 11:01 AM
  2. Apply Auto Filter in selective worksheets and Copy to new workbook
    By mclav in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-07-2011, 01:27 AM
  3. Macro to filter data and copy to another workbook
    By Roop in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-18-2011, 08:28 PM
  4. using macro to change auto filter in shared workbook
    By wish in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2010, 10:15 AM
  5. Copy from one workbook to with auto filter.
    By realdealsxbl in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-05-2009, 09:19 PM

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