+ Reply to Thread
Results 1 to 4 of 4

Distribute rows equally & roundup function mess

Hybrid View

oli_g Distribute rows equally &... 07-27-2014, 10:15 AM
patel45 Re: Distribute rows equally &... 07-27-2014, 11:42 AM
HaHoBe Re: Distribute rows equally &... 07-27-2014, 12:22 PM
oli_g Re: Distribute rows equally &... 07-27-2014, 06:09 PM
  1. #1
    Registered User
    Join Date
    05-17-2014
    MS-Off Ver
    Excel 2003
    Posts
    2

    Distribute rows equally & roundup function mess

    Hi guys,
    I have digging around this one for a while now, let me explain. I am trying to split a list according to the amount of people available on this day. So in my sheet I have X number of rows and Z number of person. X/Z will give me the amount of rows by person, easy, it works fine (the final goal is to color each block of row for each people).

    My issue is the following : Let say I have 24 rows and 10 people, the roundup function gives me 2 rows by person, and I get 10 times 2 row = 20 rows, how to distribute the last missing 4 rows ?
    My idea would be to have 8 x 2rows + 2 x 4rows = 24 rows
    I cannot figure out the mathematic way to handle this case.

    i have included my code below (I define staff and TotalRows, and it colors the rows accordingly)

    * All my datas are entered in Col A, from cell A5, that's why I have a firstrow=5

    Private Sub AutoSplit_Click()
    firstRow = 1
    staff = 10
    TotalRows = 24
    
    rwsPerStaff = WorksheetFunction.Round(TotalRows / staff, 0) 'determine the number of rows per staff
    For i = 1 To staff
    rwstart = firstRow + (i - 1) * rwsPerStaff
    rwend = rwstart + rwsPerStaff - 1
    Worksheets("Sheet1").Range("A" & rwstart & ":D" & rwstart + rwsPerStaff - 1).Select
    Selection.Interior.ColorIndex = i + 33
    Next
    Debug.Print (TotalRows + firstRow - 1)
    
    If rwend < (TotalRows + firstRow - 1) Then
    Worksheets("Sheet1").Range("A" & rwend + 1 & ":D" & (TotalRows + firstRow - 1)).Select
    Selection.Interior.ColorIndex = i + 33 - 1
    End If
    
    If rwend > (TotalRows + firstRow - 1) Then
    Worksheets("Sheet1").Range("A" & rwend + 1 & ":D" & (TotalRows + firstRow)).Select
    Selection.Interior.ColorIndex = none
    End If
    
    End Sub
    Thanks for your help !
    Oli
    Last edited by oli_g; 07-27-2014 at 10:16 AM. Reason: replace my sheet's name with Sheet1

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Distribute rows equally & roundup function mess

    can you attach a sample file with data and desired output ?
    If solved remember to mark Thread as solved

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Distribute rows equally & roundup function mess

    Hi, oli_g,

    maybe have a look at this code:
    Private Sub AutoSplit_Click()
    Dim lngSupRows      As Long
    Dim lngHelp         As Long
    Dim lngRwsPerStaff  As Long
    Dim lngRwStart      As Long
    Dim lngRwEnd        As Long
    Dim lngRC           As Long
    
    Const clngSTAFF As Long = 10
    Const clngTOTAL_ROWS As Long = 24
    
    lngRwsPerStaff = WorksheetFunction.RoundDown(clngTOTAL_ROWS / clngSTAFF, 0) 'determine the number of rows per staff
    
    lngSupRows = clngTOTAL_ROWS - lngRwsPerStaff * clngSTAFF
    lngRwStart = 5
    
    For lngRC = 1 To clngSTAFF
      With Worksheets("Sheet1")
        If lngRC <= lngSupRows Then
          lngHelp = 1
        Else
          lngHelp = 0
        End If
        lngRwEnd = lngRwStart + lngRwsPerStaff - 1 + lngHelp
        With .Range("A" & lngRwStart & ":D" & lngRwEnd)
          If lngRC Mod 2 = 1 Then
            .Interior.ColorIndex = lngRC + 33 - 1
          Else
            .Interior.ColorIndex = xlNone
          End If
        End With
      End With
      lngRwStart = lngRwEnd + 1
    Next lngRC
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    05-17-2014
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Distribute rows equally & roundup function mess

    Awesome Holger, that's exactly what I was looking for.
    Thanks for the quick answer (and the clean code)

+ 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. Distribute no of account and amount equally to callers
    By Apple Ling in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-24-2014, 06:34 AM
  2. [SOLVED] Macro to divide a range and insert them in new rows equally
    By Netaji in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-07-2014, 09:51 AM
  3. Replies: 15
    Last Post: 12-13-2013, 08:23 AM
  4. distribute names equally in front of numbers
    By rahulbawkar2006 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-20-2011, 06:18 AM
  5. Split rows equally in columns
    By cooksterni in forum Excel General
    Replies: 1
    Last Post: 06-26-2011, 10:46 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