我在 Sheet1 中有工作列表,在 Sheet2 中有5个员工姓名。当我运行一个宏时,它应该自动找出工作的数量,并且应该将工作平均分配给所有员工,并且应该将员工粘贴到那些工作中。
以下是代码
Dim x As Integer
Dim y As Integer
Range(Selection, Selection.End(xlDown)).Select
x = Selection.Rows.Count
y = Application.RoundUp(x / 5, 0)
在这里,我将如何给出范围内的y值?
答案 0 :(得分:0)
好的,试试这个:
Sub test()
Dim jRng As Range, nRng As Range
Dim i As Long, x As Long, y As Long j As Long: j = 1
With Sheet1 '~~> Change to suit
'~~> Change the range address to suit
Set jRng = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))'~~> contains jobs
Set nRng = .Range("C1", .Range("C" & .Rows.Count).End(xlUp))'~~> contains names
y = jrng.Rows.Count'~~> number of jobs
'~~> number of jobs per name
x = Application.WorksheetFunction.RoundUp(jRng.Rows.Count / nRng.Rows.Count, 0)
For i = 1 To y Step x
.Cells(i, "B").Resize(x) = nRng(j)
j = j + 1
Next
End With
End Sub
这只是基本逻辑。您可以根据自己的需要进行调整 在此代码中,您的作业列在 A列中,而 C列中的名称。 然后在 B列中填充输出。
注意:仅当数字作业可以被名称数量整除时才有效。
您必须指定如何划分多余的作业并相应地调整代码。