Macro to extract data into seperate workbooks

Kim B

Board Regular
Joined
Jun 16, 2008
Messages
232
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
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
First of all, you should avoid merging cells at all cost because they almost always cause problems for macros. I would suggest that you re-design you data so that there are no merged cells and post the revised sheet. As well, make sure that the data you post is much more representative of what your actual data looks like. You can de-sensitize any confidential data if necessary.
 
Upvote 0
The report dumps like that. After unmerging the cells to be kept are H,J,L,N. I have highlighted them. Some locations will only have one subsection, Deli. Some might have two or three. The split takes place at the next location.

Customer Dump.xlsx
ABCDEFGHIJKLMN
1Run Date: 5/4/2024 1:44:53 PMPage #: 1
2Requested Ship Date Range: x/xx/xxxx-x/xx/xxxx
3
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%
1012345421325ozRoast BeefDeliFG-ENT16916916916900100.00%100.00%
11ABC Location 2 Customer Group5,82219,1865,75619,0466614098.87%99.27%
12Produce2,14815,5122,08215,3726614096.93%99.10%
1345629652016OZVeg MixProduceFG-MAP362163621600100.00%100.00%
14DEF Location 1 Customer Group11,26529,16911,21729,073489699.57%99.67%
15Deli8,3978,3978,3978,39700100.00%100.00%
168523732568OZSaladDeliFG-GRNSAL5959595900100.00%100.00%
17Produce2,86820,7722,82020,676489698.33%99.54%
18547897566OZBroccoliProduceFG-VEG362163621600100.00%100.00%
19DEF Location 2 Customer Group17,70337,10717,37936,65132445698.17%98.77%
20Deli14,46314,46314,27114,27119219298.67%98.67%
2123547165.5CeasarDeliFG-GRNSAL1111111051056694.59%94.59%
22Produce3,24022,6443,10822,38013226495.93%98.83%
23Report Total43,476108,84842,956108,02652082298.80%99.24%
CustomerFillRateBySKU_V2
 
Upvote 0
Try:
VBA Code:
Sub CreateWorkbooks()
    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 srcWS
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Set fnd = .Range("A:A").Find("Customer Group", 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("Customer Group", LookIn:=xlValues, LookAt:=xlPart)
                If Not fnd2 Is Nothing Then
                    Workbooks.Add
                    Application.Union(.Range("H7"), .Range("J7"), .Range("L7"), .Range("N7")).Copy Range("A1")
                    Application.Union(.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
                    ActiveSheet.Name = Trim(Split(.Range("A" & fnd.Row), "Customer")(0))
                Else
                    Workbooks.Add
                    Application.Union(.Range("H7"), .Range("J7"), .Range("L7"), .Range("N7")).Copy Range("A1")
                    Application.Union(.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
                    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
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Works in that it pulls each location to it's own sheet but rows A-G are not populating so it is just showing numbers no descriptions
1715005494833.png


This is what the data really looks like. I don't hve XL2BB here sorry

1715005650682.png
 
Upvote 0
You're getting that undesired results because the script provided by mumps searches for "customer group" within column A. That string does not exist in your original workbook. According to the latest screenshot you posted, it shows "ABC LOCATION 1 CHESTER Customer". (The word "Group" is completely absent). It is definitely not the same as what you posted at the top of the thread originally. This is why mumps asked you to repost the data much more representative of what your actual data looks like.

If you're sure that each location has the word "Customer" somewhere in the title, this should work:

From line # 6 in the script that mumps gave you above:

VBA Code:
Set fnd = .Range("A:A").Find("Customer Group", LookIn:=xlValues, LookAt:=xlPart)

change to this:

VBA Code:
Set fnd = .Range("A:A").Find("Customer", LookIn:=xlValues, LookAt:=xlPart)

You just simply need to search for "Customer" instead of "Customer Group". You could also search for "Location" instead. Whatever word(s) you decide to choose, it must be contained somewhere in location name for ALL locations for the script to work.
 
Last edited:
Upvote 0
"group" is there, column wasn't big enough. But I changed the code anyway and it is still not populating the details in rows A-G
 
Upvote 0
nevermind. I just tested mumps code myself and there seems to be something missing.
 
Last edited:
Upvote 0
I took mumps code above and revised it a little bit. It works fine on my end. Try this:

VBA Code:
Sub CreateWorkbooks_v2()
    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 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
                    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
                    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

Just a quick note: I used "Location" instead of "Customer Group" as the keyword search. Either one will work as long that word (or words) appears in ALL of the various location names.
 
Upvote 0
Solution

Forum statistics

Threads
1,216,091
Messages
6,128,779
Members
449,468
Latest member
AGreen17

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