VBA to divide and assign rows

nicolas877

New Member
Joined
Jan 15, 2022
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hi forum, i came across with this problem. I need to divide certain rows by an input number to assign them to people. Like This

CASE - NAME
777
333
999
993
101
190

to this...(in this will be dividing by 3, to assign 3 people)

CASE - NAME
777 Juan
333 Juan
999 Kate
993 Kate
101 Leon
190 Leon

But the quantity of names can be variable , line 2 names ,3 names, 4 names...so there is a way to divide all the rows by certain input in a msg box an then assign a name to each division ? I only know static division by 2 in the middle with MidRow command. Thank you in advance
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about this?

2022 OTM Arrivals Mexicali Airport Weekly (version 1).xlsb
ABCD
1777JuanJuan
2333JuanKate
3999KateLeon
4993Kate
5101Leon
6190Leon
Sheet2
Cell Formulas
RangeFormula
B1:B6B1=INDEX($D$1:$D$3,((ROW()-1)/(COUNTA($A$1:$A$6)/COUNTA($D$1:$D$3)))+1)
 
Upvote 0
How about this?

2022 OTM Arrivals Mexicali Airport Weekly (version 1).xlsb
ABCD
1777JuanJuan
2333JuanKate
3999KateLeon
4993Kate
5101Leon
6190Leon
Sheet2
Cell Formulas
RangeFormula
B1:B6B1=INDEX($D$1:$D$3,((ROW()-1)/(COUNTA($A$1:$A$6)/COUNTA($D$1:$D$3)))+1)
There is a potential problem with that.
If the number of records divided by the number of people is not perfectly divisible (with no remainder), the last few records will return errors.
And I imagine that it quite commomly will not divide perfectly.

1645117428788.png
 
Upvote 0
Thank you Irobbo314 for the formula but as Joe4 says it doesnt work as is not possible to divide when there is a remainder
 
Upvote 0
We can probably use VBA to do that. Can you explain what should happen when there is an odd number like that?
In this case, would you want 3 to the first person, 3 to the second, and 2 to the third?
 
Upvote 0
OK, a few more questions.

What do you mean by this part?
.so there is a way to divide all the rows by certain input in a msg box
Do you mean that you want the VBA code to prompt them to enter the names in an input box?
If so, do you want them to enter all the names at once (separated by commas) within a single inputbox, or do you want them to repeatedly ask them for names, one after the other (so enter each name in a separate input)?

And, is it all right to do them by alternating through them, like this?
1645131954239.png
 
Upvote 0
Like this
First an input box to enter the number all the rows will be divided , lets say there are 35 rows with data and we want to divide by 3, in a input box i enter a 3 , then another input box ask for name A , then another for name B, then another for name C.......the solution then will be the first 12 rows with name A , the next 12 rows with name B , the last 11 rows with name C
 
Upvote 0
So, to assign it in the manner like I showed in my previous post would not be acceptable then?
I am pretty sure I can come up with code to do it like that.
To do it the other way is a bit more challenging.
 
Upvote 0
Assum case is from A1, name from B1
This code will ask for how many names input, then name 1, then name 2,...
Then paste the name list into column C, then assign each name in column B
VBA Code:
Option Explicit
Sub divide()
Dim Lr&, Nr&, i&, k&, arr()
Lr = Cells(Rows.Count, "A").End(xlUp).Row
Nr = InputBox("How many people?")
ReDim arr(1 To Lr, 1 To 1)
Range("C:C").ClearContents
    For i = 1 To Nr
        Range("C" & i).Value = InputBox(" person " & i & " name:")
    Next
        For i = 1 To Lr
            k = Int((i - 1) / (Int(Lr / Nr) + 1)) + 1
            arr(i, 1) = Range("C" & k).Value
        Next
Range("B1").Resize(Lr, 1).Value = arr
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,161
Messages
6,123,371
Members
449,097
Latest member
thnirmitha

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