简体   繁体   English

将 x 行数据分配给 x 人数然后颜色代码的宏

[英]Macro to allocate x Rows of Data to x Number of People and then color code

I have a task I perform I'm looking to automate for a relatively large set of data.我有一个任务,我正在寻找自动化相对较大的数据集。 Basically, I have a list of records that I need to assign out to a group of people and then color code those assignments accordingly.基本上,我有一个记录列表,我需要分配给一组人,然后相应地对这些分配进行颜色编码。

There is no set count of how many records there will be, so the number of rows is a variable.没有设置多少记录的计数,因此行数是一个变量。 The number of people is also a variable.人数也是一个变量。

The Records are in column A. The People are in Column E.记录在 A 列。人员在 E 列。

If there are 1000 records and 20 people, then assign 50 records per person by entering their name in column C.如果有 1000 条记录和 20 人,则通过在 C 列中输入他们的姓名来分配每人 50 条记录。

So how can I create a macro that will:那么如何创建一个宏:

1- count the Number of Records and divide by the Number of People 1-计算记录数并除以人数
2- use that number as the number of records to assign each person and input the person's name in column C 2- 使用该数字作为分配每个人的记录数,并在 C 列中输入该人的姓名
3- assign each person with a different Fill Color and Font Color to help distinguish between one another. 3-为每个人分配不同的填充颜色和字体颜色,以帮助区分彼此。 (is it possible to assign the same color scheme for each name in column C as well as column E?) (是否可以为 C 列和 E 列中的每个名称分配相同的配色方案?)

here's a sample of what I'm looking to accomplish这是我想要完成的一个示例

在此处输入图像描述

It's an interesting project.这是一个有趣的项目。 The code below will give you (mostly) what you're looking for.下面的代码会给你(大部分)你正在寻找的东西。 The issue with formatting is that, whereas it's possible to generate random formats – you could end up with (eg) green on green which is unusable.格式化的问题在于,虽然可以生成随机格式——你最终可能会得到(例如)绿色上的绿色,这是不可用的。 The code therefore copies whatever formats you set on each person's name in column E to all of the corresponding cells in column C.因此,该代码会将您在 E 列中为每个人的姓名设置的任何格式复制到 C 列中的所有相应单元格中。

I tested the following code with 10,000 records & 17 people each with different formats.我用 10,000 条记录和 17 个人用不同的格式测试了以下代码。 It ran in under 1 second.它在不到 1 秒的时间内运行。 Let me know how you go with it.让我知道你如何使用它 go。

Option Explicit
Sub AssignTasks()
Dim Records As Long, PasteTo As Long, People As Integer, LastP As Long
Dim balance As Integer, base As Integer, hard As Integer, p As Range
Dim AllRng As Range, BaseStart As Integer
Application.ScreenUpdating = False
 
People = Cells(Rows.Count, 3).End(xlUp).Row
If People <> 1 Then
    ActiveSheet.Range("C2:C" & People).Clear
End If

People = Cells(Rows.Count, 5).End(xlUp).Row
On Error Resume Next
ActiveSheet.Range("E2:E" & People).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
On Error GoTo 0

Records = Cells(Rows.Count, 2).End(xlUp).Row - 1
People = Cells(Rows.Count, 5).End(xlUp).Row - 1

base = Application.WorksheetFunction.RoundDown(Records / People, 0)
balance = Records - (People * base)
hard = base + 1

ActiveSheet.Range("E2:E" & balance + 1).Name = "hardrng"
ActiveSheet.Range("E" & balance + 2 & ":E" & People + 1).Name = "easyrng"

If balance = 0 Then GoTo SkipHard

For Each p In ActiveSheet.Range("hardrng")    
    PasteTo = Cells(Rows.Count, 3).End(xlUp).Row + 1
    Set AllRng = ActiveSheet.Range("C" & PasteTo)
    AllRng.Resize(hard, 1).Value = p.Value    
Next p

SkipHard:
For Each p In ActiveSheet.Range("easyrng")    
    PasteTo = Cells(Rows.Count, 3).End(xlUp).Row + 1
    Set AllRng = ActiveSheet.Range("C" & PasteTo)
    AllRng.Resize(hard - 1, 1).Value = p.Value    
Next p

'Copy the formats
People = Cells(Rows.Count, 5).End(xlUp).Row
LastP = Cells(Rows.Count, 3).End(xlUp).Row

For Each p In ActiveSheet.Range("E2:E" & People)        
    Sheet1.Range("C:C").AutoFilter Field:=1, Criteria1:="" & p
    p.Copy
    On Error Resume Next
        Sheet1.Range("C2:C" & LastP).SpecialCells(xlCellTypeVisible).PasteSpecial xlFormats            
    On Error GoTo 0
    Application.CutCopyMode = False    
Next p

Sheet1.AutoFilterMode = False    
Application.ScreenUpdating = True
End Sub

Note that you say the records are in column A, but your image shows them in column B. This code uses column B - easily changed.请注意,您说记录在 A 列中,但您的图像在 B 列中显示它们。此代码使用 B 列 - 很容易更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM