如果有多个最小值,如何调整代码

时间:2015-11-24 22:41:57

标签: excel vba excel-vba

我正在尝试编写一个代码,该代码将查看城市中的呼叫,然后在城市中指派一个团队来补救呼叫。有7个团队位置,团队由哪个团队负责最轻的工作分配。如果两个团队具有相同的工作量,则将其分配给最近城市的团队。

我在考虑循环如何计算最小工作量时遇到问题,但是考虑到有超过一分钟的时间,然后决定使用最近的最小位置。

现在我有一张工作表,其中包含通话日期和每次通话完成的天数。我有一个数组(N4:N10),显示每个团队位置的最后完成日期。现在我的循环只是占用那些完成日期的最小值并将相应的团队分配给呼叫,然后将最后完成日期增加到完成最后一次呼叫的预期天数,但是它没有考虑到超过最低限度。

Sub TeamAssignment()
   Dim lastRow As Integer
   Dim i As Integer
   lastRow = Worksheets("Calc").Range("E" & Rows.Count).End(xlUp).Row
   Dim SmallestWorkload As Date
   Dim TeamAssigned As String

   For i = 3 To lastRow
      'find smallest workload and put into sheet
      SmallestWorkload = Application.WorksheetFunction.Min(Sheets("Calc").Range("N4:N10"))
      TeamAssigned = SmallestWorkload.Offset(0, 1).Value
      Range(i, "F").Value = TeamAssigned

      If TeamAssigned = "Miami" Then
         MIAcompletion = SmallestWorkload + Range(i, "I").Value
         Range("N9").Value = MIAcompletion
      ElseIf TeamAssigned = "Jacksonville" Then
         JAXcompletion = SmallestWorkload + Range(i, "I").Value
         Range("N5").Value = JAXcompletion
      ElseIf TeamAssigned = "New Smyrna Beach" Then
         NSBcompletion = SmallestWorkload + Range(i, "I").Value
         Range("N6").Value = NSBcomplation
      ElseIf TeamAssigned = "Ocala" Then
         OCALAcompletion = SmallestWorkload + Range(i, "I").Value
         Range("N7").Value = OCALAcompletion
      ElseIf TeamAssigned = "St. Petersburg" Then
         SPcompletion = SmallestWorkload + Range(i, "I").Value
         Range("N8").Value = SPcompletion
      ElseIf TeamAssigned = "Gainesville" Then
         GNVcompletion = SmallestWorkload + Range(i, "I").Value
         Range("N4").Value = GNVcompletion
      Else
         PSLcompletion = SmallestWorkload + Range(i, "I").Value
         Range("N10").Value = PSLcompletion
      End If
   Next i
End Sub

1 个答案:

答案 0 :(得分:1)

下面是一个示例,说明如何使用vba代码尝试查看循环中的两个条件。

然后你必须在最后将变量分配给你希望他们进入的单元格,并添加代码来更新所选择的最终团队,但也许它会给你一些想法。

Sub TeamAssignment()

    ' Assuming column N has the workload date, column O has the Team, and column P has the distance in miles
    Dim lastRow As Integer
    Dim i As Integer
    lastRow = Worksheets("Calc").Range("E" & Rows.Count).End(xlUp).Row
    Dim TeamAssigned As String, SmallestWorkload As Date, ClosestTeam As Double

    ' Choose the first team to begin with
    SmallestWorkload = Range("N3").Value: TeamAssigned = Range("O3").Value: ClosestTeam = Range("P3").Value

    'find smallest workload and put into sheet
    For i = 3 To lastRow
        Select Case Range("N" & i).Value
            Case Is < SmallestWorkload
                ' If we find a smaller workload, then assign it to this team
                SmallestWorkload = Range("N" & i).Value: TeamAssigned = Range("O" & i).Value: ClosestTeam = Range("P" & i).Value
            Case Is = SmallestWorkload
                ' If the workload is the same, then determine if it should be assigned based on the distance
                If Range("P" & i).Value < ClosestTeam Then _
                    SmallestWorkload = Range("N" & i).Value: TeamAssigned = Range("O" & i).Value: ClosestTeam = Range("P" & i).Value
        End Select
    Next i


End Sub