+ Reply to Thread
Results 1 to 2 of 2

VBA to split/copy rows based on max quantities

Hybrid View

  1. #1
    Registered User
    Join Date
    06-13-2014
    Posts
    1

    VBA to split/copy rows based on max quantities

    Hi,

    I have an order form that i would like to be able to split data in a row based on the quantities of certain items. Once the max is hit for that item i would need it to copy the remaining down until there is no more of that item.

    Small items have a max quantity of 10
    Large items have a max of 5

    Sorry, if its hard to understand what i'm asking. I have attached a workbook with what is the beginning data and the end result of what ill be needing. I don't have much experience with VBA but i can learn quickly and adapt pretty easily. Any help would be greatly appreciated. Thanks.
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to split/copy rows based on max quantities

    Hello

    Welcome to the Forum!

    The following macro has been added to your workbook. There is button on the sheet to run it.

    Sub Macro1()
    
        Dim Data    As Variant
        Dim MaxCnt  As Long
        Dim n       As Long
        Dim r       As Long
        Dim Rng     As Range
        Dim RngBeg  As Range
        Dim rowcnt  As Long
        Dim Wks     As Worksheet
        
            Set Wks = ActiveSheet
            
            Set RngBeg = Wks.Cells.Find("Qty", , xlValues, xlWhole, xlByColumns, xlNext, False, False, False)
                If RngBeg Is Nothing Then
                    MsgBox "Quantity column not found.", vbExclamation
                    Exit Sub
                End If
            
            Set Rng = RngBeg.CurrentRegion
                If Rng.Rows.Count = 1 Then
                    MsgBox "There is data in the table.", vbExclamation
                    Exit Sub
                End If
                
                Set Rng = Intersect(Rng, Rng.Offset(1, 0))
                
                Data = Rng.Value
                
                For n = LBound(Data, 1) To UBound(Data, 1)
                
                    Select Case LCase(Data(n, 3))
                        Case Is = "small": MaxCnt = 10
                        Case Is = "large": MaxCnt = 5
                        Case Else: MaxCnt = 1
                    End Select
                    
                    If MaxCnt > 1 Then
                        rowcnt = (Data(n, 1) \ MaxCnt)
                        r = (Data(n, 1) Mod MaxCnt)
                        If r > 0 Then rowcnt = rowcnt + 1
                    Else
                        rowcnt = rowcnt + 1
                    End If
                    
                    Data(n, 1) = MaxCnt
                    
                    Set Rng = Rng.Resize(RowSize:=rowcnt)
                    Rng.Value = Application.Index(Data, n, 0)
                    
                    If r <> 0 Then Rng.Cells(Rng.Rows.Count, 1) = r
                    
                    Set Rng = Rng.Offset(rowcnt, 0)
                    
                Next n
                
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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] VBA Split Cell Contents to New Rows & Copy Cells containing single values to the new rows
    By jaimelwilson in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-14-2017, 05:30 PM
  2. split/separate rows based on criteria and send those rows by mail
    By katu in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-09-2014, 08:59 AM
  3. Need VBA code to split quantities based on availability in another table
    By adelkam in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2013, 01:45 AM
  4. Copy only rows with quantities value into another worksheet
    By tommygray in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-05-2013, 09:24 AM
  5. [SOLVED] macro to create rows of data based on divided quantities
    By MSApprentice in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-25-2011, 12:19 PM
  6. How to split an Excel file based on rows?
    By Delta223 in forum Excel General
    Replies: 4
    Last Post: 08-19-2009, 11:15 PM
  7. Copy rows in a split way
    By bdn435 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2007, 09:55 AM

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