Randomly Assign Employees to Tasks

前端 未结 3 743
轻奢々
轻奢々 2021-01-21 14:35

This is a follow-up to a previous question that I had. I was provided an answer, but due to my own inexperience and inability, I can\'t seem to implement it properly.

My

3条回答
  •  走了就别回头了
    2021-01-21 15:17

    I created a solution for you which might help you to develop further also in general understanding of programming.

    With my solution you dont need to shuffle your employees beforehand and you will use some stuff you might have not used before. First of all I created a new Class Module called Employee which looks like this:

    Private p_name As String
    Private p_task As String
    
    Public Property Get Name() As String
        Name = p_name
    End Property
    
    Public Property Let Name(ByVal value As String)
    p_name = value
    End Property
    
    Public Property Get Task() As String
        Task = p_task
    End Property
    
    Public Property Let Task(ByVal value As String)
    p_task = value
    End Property
    

    This is only a small class to hold an employeename and a task. In a normal Module I added a method called ShuffleTasks with 2 collections as parameters. A Collection is a slightly more comfortable and therefor slightly heavier and slower version of an array.

    Private Sub ShuffleTasks(t As Collection, emp As Collection)
    Dim i As Integer
    Dim count As Integer
    Dim employ As employee
    count = emp.count
    Dim remIndex  As Integer
    For i = 1 To count
    'randomize
    Randomize
    'get a random index from tasks by its count
    remIndex = Int((t.count) * Rnd + 1)
    'add the task to the employee list
    emp.Item(i).Task = t.Item(remIndex)
    'remove the task so it wont be assigned again
    t.Remove (remIndex)
    Next
    End Sub
    

    The first parameter is a collection of the tasks(which is just a string with the name), the second the collection of the employees. The second one will also the one being used as the result. Then I iterate through all employees and generate a random integer between 1 and the count of the tasks. I'll add the task to the current employee in the collection and REMOVE it from the tasklist. In the next iteration the numbers of tasks will be -1 and again randomized chosen from the amount of items in the collection.

    Then I modified your EmpArray Method to fill some data from a sheet and call the ShuffleTasks method

    Sub EmpArray()
    ' This stores the column of Emps as an Collection
    
        Dim sEmployees As New Collection, sTasks As New Collection ' initial storage array to take values
        Dim i As Long
        Dim j As Long
        Dim s As Variant
        Dim lrow As Long
        Dim emp As employee
        lrow = Cells(Rows.count, "M").End(xlUp).Row ' The amount of stuff in the column
    
        For i = lrow To 6 Step -1
            If (Not IsEmpty(Cells(i, 13).value)) Then ' checks to make sure the value isn't empty
                j = j + 1
                'Storage(j) = Cells(i, 13).Value
                Set emp = New employee
                emp.Name = Cells(i, 13).value
                sEmployees.Add emp
            End If
        Next i
    ' This stores the column of Tasks as an Collection
    ' I assume it is column 9
    lrow = Cells(Rows.count, "I").End(xlUp).Row ' The amount of stuff in the column
        For i = lrow To 6 Step -1
            If (Not IsEmpty(Cells(i, 9).value)) Then ' checks to make sure the value isn't empty
                j = j + 1
                sTasks.Add Cells(i, 9).value
    
            End If
        Next i
    ShuffleTasks sTasks, sEmployees
    For Each emp In sEmployees
        Debug.Print (emp.Name & ": " & emp.Task)
    Next
    
    End Sub
    

    As you can see the modifications on the Collection will show you each time a new employee name and task. Keep in mind that it is ofc not true random. Also the collection of tasks will have less items after the method ShuffleTasks. I just wanted to show you an approach which is basically working a bit with data in vba. You only load data from the sheet, then manipulate that in pure vba objects. The results can also be written back to the sheet, I just print them to Debug Window in your vba editor.

    Hope this helps. It is for sure a quick and dirty solution and I also didnt cover all aspects of Collections and also Parameters and ByVal vs ByRef etc. But maybe this will inspire you a bit ;)

提交回复
热议问题