Macro to extract data into seperate workbooks

Kim B

Board Regular
Joined
Jun 16, 2008
Messages
233
Office Version
  1. 365
HI all. This has two parts.

This data is pulled down in a group format. I have given 1 line under each category for this scenario. It has merged cells as well. I have expanded the grouping from row 8 down. Columns A-K are merged cells. I would think auto fit and deleting of columns would come first then extract what's left would be easier but whichever order works best is fine.

1. Need to extract each location to it's own workbook (locations all have cutomer group at the end of their names. Everything under a location goes with that location until the next location is listed, and
2. auto fit columns and delete columns N,P,Q,R,T so leaving O,Q,S,U.

Thank you for your help.


Customer Dump.xlsx
ABCDEFGHIJKLMNOPQRSTU
1Run Date: 5/4/2024 1:44:53 PMPage #: 1
2
3Requested Ship Date Range: x/xx/xxxx-x/xx/xxxx
4
5
6
7Item #External Item #Pack SizeItem DescTypeItem GroupOrder Qty CaseUnit or LB OrderedShip Qty CaseUnit or LB ShipShip Var - CasesShip Var - Unit/LbsCase Fill %Unit or LB Fill Rate
8ABC Location 1 Customer Group8,68623,3868,60423,2568213099.06%99.44%
9Deli6,1866,1866,1526,152343499.45%99.45%
10XXXXXXXXXXOZXXXXDeliFG-ENT16916916916900100.00%100.00%
11Produce2,50017,2002,45217,104489698.08%99.44%
12ABC Location 2 Customer Group5,82219,1865,75619,0466614098.87%99.27%
13Deli3,6743,6743,6743,67400100.00%100.00%
14Produce2,14815,5122,08215,3726614096.93%99.10%
15XXXXXXXXXXOZXXXXProduceFG-MAP362163621600100.00%100.00%
16DEF Location 1 Customer Group11,26529,16911,21729,073489699.57%99.67%
17Deli8,3978,3978,3978,39700100.00%100.00%
18XXXXXXXXXXOZXXXXDeliFG-GRNSAL5959595900100.00%100.00%
19Produce2,86820,7722,82020,676489698.33%99.54%
20XXXXXXXXXXOZXXXXProduceFG-VEG362163621600100.00%100.00%
21DEF Location 2 Customer Group17,70337,10717,37936,65132445698.17%98.77%
22Deli14,46314,46314,27114,27119219298.67%98.67%
23XXXXXXXXXXOZXXXXDeliFG-GRNSAL1111111051056694.59%94.59%
24Produce3,24022,6443,10822,38013226495.93%98.83%
25Report Total43,476108,84842,956108,02652082298.80%99.24%
CustomerFillRateBySKU_V2
 
I noticed that column A did not look right after the locations were split off into their own separate workbooks. Do you have wrap text enabled in column A where those Customer groups are located?

If so, the following revised code might work better for autofit.

VBA Code:
Sub CreateWorkbooks_v2_A()
    Application.ScreenUpdating = False
    Dim lRow As Long, fnd As Range, fnd2 As Range, sAddr As String, srcWS As Worksheet
    Set srcWS = Sheets("CustomerFillRateBySKU_V2")
    With ActiveWorksheet
        Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).WrapText = False
    End With
    With srcWS
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Set fnd = .Range("A:A").Find("Location", LookIn:=xlValues, LookAt:=xlPart)
        If Not fnd Is Nothing Then
            sAddr = fnd.Address
            Do
                Set fnd2 = .Range("A" & fnd.Row + 1 & ":A" & lRow - 1).Find("Location", LookIn:=xlValues, LookAt:=xlPart)
                If Not fnd2 Is Nothing Then
                    Workbooks.Add
                    Application.Union(.Range("A7"), .Range("H7"), .Range("J7"), .Range("L7"), .Range("N7")).Copy Range("A1")
                    Application.Union(.Range("A" & fnd.Row & ":A" & fnd2.Row - 1), .Range("H" & fnd.Row & ":H" & fnd2.Row - 1), .Range("J" & fnd.Row & ":J" & fnd2.Row - 1), .Range("L" & fnd.Row & ":L" & fnd2.Row - 1), .Range("N" & fnd.Row & ":N" & fnd2.Row - 1)).Copy Range("A2")
                    Columns.AutoFit
                    Rows(1).AutoFit
                    ActiveSheet.Name = Trim(Split(.Range("A" & fnd.Row), "Customer")(0))
                Else
                    Workbooks.Add
                    Application.Union(.Range("A7"), .Range("H7"), .Range("J7"), .Range("L7"), .Range("N7")).Copy Range("A1")
                    Application.Union(.Range("A" & fnd.Row & ":A" & lRow - 1), .Range("H" & fnd.Row & ":H" & lRow - 1), .Range("J" & fnd.Row & ":J" & lRow - 1), .Range("L" & fnd.Row & ":L" & lRow - 1), .Range("N" & fnd.Row & ":N" & lRow - 1)).Copy Range("A2")
                    Columns.AutoFit
                    Columns("B:B").ColumnWidth = 8.86
                    Rows(1).AutoFit
                    ActiveSheet.Name = Trim(Split(.Range("A" & fnd.Row), "Customer")(0))
                End If
                Set fnd = .Range("A:A").FindNext(fnd)
            Loop While fnd.Address <> sAddr
            sAddr = ""
        End If
    End With
End Sub
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
This assumes the layout in Post #1 is correct ie heading row is on row 7 and the sheet has merged cells.
It is not as compact as the other suggestions put forward but should do what you want.
It will save the created workbooks in the same folder as the workbook containing the code.
It uses a temporary workbook in which to unmerge cells and remove columns.

VBA Code:
Sub CreateWorkbooks_Alternative()

    Dim srcWB As Workbook, unmergeWB As Workbook, newWB As Workbook
    Dim srcWS As Worksheet, unmergeWS As Worksheet, newWS As Worksheet
    Dim sFldr As String
    Dim unmergeRng As Range, unmergeArr As Variant
    Dim strFind As String
    Dim SectName As String
    Dim lRow As Long, lcol As Long, lRowSectStart As Long, lRowSectEnd As Long
    Dim hdgRow As Long
    Dim i As Long
      
    Application.ScreenUpdating = False
    strFind = "Customer Group"
    Set srcWB = ThisWorkbook
    Set srcWS = srcWB.Sheets("CustomerFillRateBySKU_V2")            ' <--- Change this as required
    sFldr = srcWB.Path                                              ' <--- Change this as required
    If Right(sFldr, 1) <> Application.PathSeparator Then sFldr = sFldr & Application.PathSeparator
    
    ' Create a temporary workbook in which to unmerge the cells and remove unwanted columns
    srcWS.Copy
    Set unmergeWB = ActiveWorkbook
    Set unmergeWS = unmergeWB.ActiveSheet
    With unmergeWS
        hdgRow = 7
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Set unmergeRng = .Range("A1:U" & lRow)
        unmergeRng.WrapText = False
        unmergeRng.MergeCells = False
        ' Move headings to the left - currently in blank columns to be deleted
        .Range("B1:U4").Copy Destination:=.Range("B1:U4").Offset(, -1)
        ' Delete blank and columns not required
        .Range("b1,d1,f1,h1:j1,m1,n1,p1,r1,t1").EntireColumn.Delete     ' <--- Change this as required
        lcol = .Cells(hdgRow, Columns.Count).End(xlToLeft).Column
        Set unmergeRng = .Range("A1", .Cells(lRow, lcol))
        unmergeRng.Offset(6).Columns.AutoFit
        unmergeArr = unmergeRng.Columns(1)
    End With
    
    ' Create a workbook for each section
    For i = hdgRow + 1 To lRow
        If InStr(1, unmergeArr(i, 1), strFind, vbTextCompare) > 0 _
            And lRowSectStart = 0 Then
                lRowSectStart = i
        ElseIf InStr(1, unmergeArr(i, 1), strFind, vbTextCompare) > 0 Or i = lRow Then
            lRowSectEnd = i - 1
            SectName = Replace(unmergeArr(lRowSectStart, 1), " " & strFind, "")
            ' Create new workbook
            Set newWB = Workbooks.Add(xlWBATWorksheet)
            Set newWS = newWB.Worksheets(1)
            ' Copy headings
            unmergeRng.Resize(hdgRow).Copy Destination:=newWS.Range("A1")
            ' Copy SectStart to End
            With unmergeWS
                .Range(.Cells(lRowSectStart, 1), .Cells(lRowSectEnd, lcol)).Copy _
                    Destination:=newWS.Range("A" & hdgRow + 1)
                .Range(.Cells(lRowSectStart, 1), .Cells(lRowSectEnd, lcol)).Copy
                    newWS.Range("A" & hdgRow + 1).PasteSpecial Paste:=xlPasteColumnWidths
                newWS.Name = SectName
                
                lRowSectStart = i
            End With
            newWS.Range("A" & hdgRow + 1).Select
            newWB.SaveAs Filename:=sFldr & SectName & ".xlsx", FileFormat:=xlOpenXMLWorkbook
            newWB.Close
        End If
    Next i
    
    Application.DisplayAlerts = False
    unmergeWB.Close savechanges:=False
    Application.DisplayAlerts = True
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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