我想将零件平均分配给分析师。
支持我的部分列今天包含5个零件和3位分析师。
Sheet1
指望的输出排序列部分。
以下宏将所有零件号分配给一个分析师。
Sub AssignAnalysts()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lastRow1 As Long, lastRow2 As Long
Dim partRange As Range, analystRange As Range
Dim i As Long, j As Long
Dim analystIndex As Long
' Set references to the worksheets
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
' Find the last rows with data in each sheet
lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
' Set ranges for Part and Analyst columns
Set partRange = ws1.Range("A2:A" & lastRow1)
Set analystRange = ws2.Range("A2:A" & lastRow2)
' Initialize analystIndex
analystIndex = 1
' Loop through each row in the Part column
For i = 1 To partRange.Rows.Count
' Get the current part
part = partRange.Cells(i, 1).Value
' Loop through analysts and assign them to parts
For j = 1 To analystRange.Rows.Count
' Get the current analyst
analyst = analystRange.Cells(j, 1).Value
' Assign analyst to the part and increment analystIndex
ws1.Cells(i + (j - 1) * lastRow1, 3).Value = analyst
Next j
Next i
End Sub
循环内部/第二个意味着最后一个分析师将始终分配给每个部分。这是不需要的。我认为这可以简化为:
' Loop through each row in the Part column
j = 1 ' point to the first analyst
For i = 2 To lastRow1 ' scroll through ws1 (parts)
' Get the analyst
analyst = analystRange.Cells(j, 1).Value
' Assign the analyst to the part
ws1.Cells(i, 2).value = analyst
' point to next analyst. If end of list, cycle to top.
j = j + 1
if j > analystRange.Rows.Count then
j = 1
end if
Next i
如果没有一些Excel命令,您可以在没有VBA的情况下做到这一点。 在零件列表中插入了一个新列,并将它们从单元格A列中的1中编号。
拖拉分析师名称范围(C2:C4)直到表末端。
在编号列上的表格以返回原始列表。 特定零件/分析师的数量可能会有很小的差异,但总的来说,这种方法给出了同样分配的结果。