VBA to auto filter data based on department and copy into new workbook

ChanL

Board Regular
Joined
Apr 8, 2021
Messages
65
Office Version
  1. 2019
Platform
  1. Windows
I have a set of data where need to filter based on department value (i.e. department 1 , department 2) and after filtered, I will need to copy the data together with my header in row 1 and row 2 into a new workbook and rename it with the department name. The code need to be loop through the unique value of column D for all the unique department name.
1684765283959.png


Here are the code I written, it able to create new workbook bt it didn't copy only data for that particular department, in fact it copy the whole thing and just turn on the filter
VBA Code:
Sub FilterCopyPaste()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim filterRange As Range
    Dim filteredData As Range
    Dim departmentCell As Range
    Dim newWorkbook As Workbook
    Dim newFilePath As String
    
    ' Set the workbook and worksheet variables
    Set wb = ThisWorkbook
    Set ws = wb.ActiveSheet
    
    ' Set the range to be filtered based on the department column
    Set filterRange = ws.Range("D4").CurrentRegion
    filterRange.AutoFilter Field:=4, Criteria1:="<>"
    
    ' Loop through each unique department
    For Each departmentCell In ws.Range("D4", ws.Range("D" & ws.Rows.Count).End(xlDown)).SpecialCells(xlCellTypeVisible)
        ' Copy the filtered data
        ws.Copy
        ' Create a new workbook and paste the filtered data as values
        Set newWorkbook = Workbooks.Add
        newWorkbook.Worksheets(1).Range("A1").PasteSpecial Paste:=xlPasteValues
        ' Rename the new workbook with the department value
        newWorkbook.SaveAs wb.Path & "\" & departmentCell.Value & ".xlsx"
        newWorkbook.Close SaveChanges:=False
    Next departmentCell
    
    Application.ScreenUpdating = False
    
    ' Clear the filters
    filterRange.AutoFilter
    
    ' Activate the original workbook
    wb.Activate

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
You are close.
If you assume the list does not contain duplicate departments, then this should work:

VBA Code:
Sub FilterCopyPaste()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim filterRange As Range
    Dim filteredData As Range
    Dim departmentCell As Range
    Dim newWorkbook As Workbook
    Dim newFilePath As String
   
    ' Set the workbook and worksheet variables
    Set wb = ThisWorkbook
    Set ws = wb.ActiveSheet
   
    ' Set the range to be filtered based on the department column
    Set filterRange = ws.Range("D4").CurrentRegion
   
    ' Loop through each unique department
    For Each departmentCell In ws.Range("D4", ws.Range("D" & ws.Rows.Count).End(xlDown)).SpecialCells(xlCellTypeVisible)
       
        filterRange.AutoFilter Field:=4, Criteria1:=departmentCell.Value
       
        ws.UsedRange.SpecialCells(xlCellTypeVisible).Copy
       
        ' Create a new workbook and paste the filtered data as values
        Set newWorkbook = Workbooks.Add
        newWorkbook.Worksheets(1).Range("A1").PasteSpecial Paste:=xlPasteValues
        ' Save the new workbook with the department value
        newWorkbook.SaveAs wb.Path & "\" & departmentCell.Value & ".xlsx"
        newWorkbook.Close SaveChanges:=False
    Next departmentCell
   
    Application.ScreenUpdating = False
   
    ' Clear the filters
    filterRange.AutoFilter
   
    ' Activate the original workbook
    wb.Activate

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top