Filter & Copy, Paste Data in New Excel File From Branch List Sheet & Save That Same in Folder with VBA Loop

aarifar08

New Member
Joined
May 30, 2024
Messages
2
Office Version
  1. 2021
I want to filter and split the data based on branch name (Branch List sheet), and also want to save that new excel workbook for each branch as per the same name as mentioned in the Branch List sheet. Please help me with LOOP function to complete my code.

1717068444603.png
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Please help me with LOOP function to complete my code.

Post your code.

The code here fulfills a similar request to yours and shows how to loop through distinct 'names', filtering the sheet by each name and saving the visible rows to a new workbook on the Desktop. It should be easily adaptable to your request.

 
Upvote 0
Post your code.

The code here fulfills a similar request to yours and shows how to loop through distinct 'names', filtering the sheet by each name and saving the visible rows to a new workbook on the Desktop. It should be easily adaptable to your request.


Below my Code:

Sub Split_Branch()

Range(Selection, Selection.End(xlToRight)).Select

Range(Selection, Selection.End(xlDown)).Select

Selection.AutoFilter

Sheets("Branch_List").Select

Range("B2").Select

ActiveCell.FormulaR1C1 = "AMALNER SME"

Sheets("All Products Due List").Select

Range("B1").Select

ActiveSheet.Range("$A$1:$AL$500000").AutoFilter Field:=2, Criteria1:= _

"AMALNER SME"

Selection.End(xlToLeft).Select

Range(Selection, Selection.End(xlToRight)).Select

Range(Selection, Selection.End(xlDown)).Select

Selection.Copy

Workbooks.Add

ActiveSheet.Paste

Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _

SkipBlanks:=False, Transpose:=False

Selection.Columns.AutoFit

Range("A2").Select

Application.CutCopyMode = False

ActiveCell.FormulaR1C1 = "1"

Range("A3").Select

ActiveCell.FormulaR1C1 = "=R[-1]C+1"

Range("A3").Select

Selection.Copy

Range(Selection, Selection.End(xlDown)).Select

Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _

SkipBlanks:=False, Transpose:=False

Range("B2").Select

Application.CutCopyMode = False

ActiveCell.FormulaR1C1 = "AMALNER SME"

Range("A1").Select

ChDir "C:\Users\hp\Desktop\@Split Branch"

ActiveWorkbook.SaveAs Filename:= _

"C:\Users\hp\Desktop\@Split Branch\AMALNER SME.xlsx", FileFormat:= _

xlOpenXMLWorkbook, CreateBackup:=False

ActiveWindow.Close



End Sub
 
Upvote 0
Try this macro. It assumes the branch names are in the "Branch_List" sheet in column A starting at A2 and there are no duplicates.

VBA Code:
Public Sub Split_Sheet_By_Branches()
    
    Dim destFolder As String
    Dim Branches As Variant, Branch As Variant
    Dim filteredCells As Range
    Dim BranchWorkbook As Workbook
    Dim AutoFilterWasOn As Boolean
    
    destFolder = "C:\Users\hp\Desktop\@Split Branch\"
    
    With ActiveWorkbook.Worksheets("Branch_List")
        Branches = Application.WorksheetFunction.Transpose(.Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value)
    End With
    
    Application.ScreenUpdating = False

    With ActiveWorkbook.Worksheets("All Products Due List")
    
        For Each Branch In Branches
    
            'Filter on column B to show only rows for this Branch

            .UsedRange.AutoFilter Field:=2, Operator:=xlFilterValues, Criteria1:="=" & Branch
            Set filteredCells = .UsedRange.SpecialCells(xlCellTypeVisible)
            
            'Copy filtered cells to new workbook and put 1, 2, 3... number series in column A
            
            Set BranchWorkbook = Workbooks.Add(xlWBATWorksheet)
            filteredCells.Copy BranchWorkbook.Worksheets(1).Range("A1")
            With BranchWorkbook.Worksheets(1)
                .Range("A2").Value = 1
                With .Range("A2", .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row))
                    .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1
                End With
            End With
            BranchWorkbook.Worksheets(1).Range("A1").CurrentRegion.EntireColumn.AutoFit
            Application.DisplayAlerts = False 'suppress warning if file already exists
            BranchWorkbook.SaveAs destFolder & Branch & ".xlsx", xlOpenXMLWorkbook
            Application.DisplayAlerts = True
            BranchWorkbook.Close False
            
        Next
    
        'Restore autofilter if it was on
        
        .AutoFilter.ShowAllData
        If Not AutoFilterWasOn Then .AutoFilterMode = False
        
    End With
    
    Application.ScreenUpdating = True
    
    MsgBox "Done"

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,932
Messages
6,175,466
Members
452,646
Latest member
tudou

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