+ Reply to Thread
Results 1 to 4 of 4

Break customer orders based on quantity

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-14-2012
    Location
    Davenport, Iowa
    MS-Off Ver
    Excel 365
    Posts
    206

    Break customer orders based on quantity

    Hi,

    I have a spreadsheet with a list of customer items they wish to purchase and the quantity of each item and their respective due dates. I am looking to see if a macro can look at the quantity and split each item out into individual line items with a quantity of 1.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Break customer orders based on quantity

    
    Sub Test()
    
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("D2:D" & LR) _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A2:D" & LR)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
    On Error GoTo Quit
    
    Search:
    Z = Columns(4).Find(1, LookIn:=xlValues, Lookat:=xlWhole).Row
    If Z = 2 Then GoTo Quit
    Q = Cells(Z - 1, 4).Value
    Rows(Z & ":" & Z + Q - 2).Insert
    Range(Cells(Z - 1, 1), Cells(Z - 1, 4)).Copy Destination:=Range(Cells(Z, 1), Cells(Z + Q - 2, 4))
    Range(Cells(Z - 1, 4), Cells(Z + Q - 2, 4)).Value = 1
    GoTo Search
    
    Quit:
    
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("A2:A" & LR) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A2:D" & LR)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    On Error GoTo 0
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Break customer orders based on quantity

    Here you go. It's a matter of managing two loops.

    Sub BreakItems()
    Dim sht1 As Worksheet       ' Sheet containing the grouped items
    Dim sht2 As Worksheet       ' Sheet containing the broken out items
    Dim LRow1 As Long           ' Last row on sheet 1
    Dim RowNum2 As Long         ' Working row number sheet 2
    Dim i1 As Long              ' Row index on sheet 1
    Dim i2 As Long              ' Row index on sheet 2
    
    Dim Item As String          ' Item
    Dim Customer As String      ' Customer
    Dim DueDate As Date         ' Due Date
    Dim Qty As Long             ' Quantity
    
    ' Initalize variables
    Set sht1 = Sheets("Sheet1")
    Set sht2 = Sheets("Sheet2")
    RowNum2 = 2
    
    ' Clear old data
    sht2.Cells.ClearContents
    
    ' Make titles
    sht2.Range("A1") = "Item"
    sht2.Range("B1") = "Customer"
    sht2.Range("C1") = "Due Date"
    
    ' get the last row on sheet 1
    LRow1 = sht1.Range("A" & Rows.Count).End(xlUp).Row
    
    ' Loop through the items and
    For i1 = 4 To LRow1
        Item = sht1.Cells(i1, "A")           ' Item
        Customer = sht1.Cells(i1, "B")       ' Customer
        DueDate = sht1.Cells(i1, "C")        ' Due Date
        Qty = sht1.Cells(i1, "D")            ' Quantity
        
        ' Loop thorugh sheet 2
        For i2 = RowNum2 To RowNum2 + Qty - 1
            sht2.Cells(i2, "A") = Item
            sht2.Cells(i2, "B") = Customer
            sht2.Cells(i2, "C") = DueDate
        Next i2
        RowNum2 = RowNum2 + Qty
    Next i1
    
    End Sub
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  4. #4
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Break customer orders based on quantity

    Or
    Sub VenA()
    Dim j As Long, jj As Long, ar
      ar = Sheets("Sheet1").Cells(2, 1).CurrentRegion
      ReDim ar1(2, 0)
        For j = 3 To UBound(ar)
          For jj = 1 To ar(j, 4)
            ar1(0, UBound(ar1, 2)) = ar(j, 1)
            ar1(1, UBound(ar1, 2)) = ar(j, 2)
            ar1(2, UBound(ar1, 2)) = CDbl(ar(j, 3))
            ReDim Preserve ar1(2, UBound(ar1, 2) + 1)
          Next jj
        Next j
      With Sheets("Sheet1").Cells(1, 15)
      .Resize(, 3).Value = Application.Index(ar, 2, Array(1, 2, 3))
      .Offset(1).Resize(UBound(ar1, 2), 3) = Application.Transpose(ar1)
      End With
    End Sub

+ 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. [SOLVED] Split orders based on quantity and indicator
    By sriley5 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-14-2019, 11:12 PM
  2. Google Sheets - line up orders by turnover or quantity
    By Ymmies in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 1
    Last Post: 01-18-2019, 08:28 AM
  3. Calculate time difference between two orders per customer
    By bobbielin in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-13-2013, 02:36 PM
  4. Replies: 0
    Last Post: 10-25-2012, 09:52 AM
  5. Replies: 11
    Last Post: 10-25-2012, 12:26 AM
  6. Help with sorting customer orders and outputting them to a new sheet
    By JBrez in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 06-17-2012, 07:21 PM
  7. [SOLVED] tracking customer orders for ad specialty distributror
    By Abie Ross in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 09-23-2005, 01:05 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