我创建了一个宏文件来运行贪婪算法,这是一个将具有最大值的工作分配给空闲服务器的启发式过程。宏应该为运行贪婪算法的两个工作站分配26个工作。
首先,我必须确定是否已分配任务,然后我必须确定哪个未分配的任务具有最长的任务时间并继续前进。这应该是一个非常简单的代码,但我无法从电子表格中读取任务时间,范围从(C2:C27)。我写了下面的代码,但是在我用两个**标记的行上得到Run Time Error '13': Type mismatch
:
Sub GreedyAlgorithm()
Dim totalA As Integer
Dim totalB As Integer
Dim nbA As Integer
Dim nbB As Integer
Dim maxRN As Integer
Dim maxTT As Integer
totalA = 0
totalB = 0
nbA = 0
nbB = 0
For i = 1 To 26
maxRN = 0
maxTT = 0
For j = 2 To 27
If IsEmpty(Sheet2.Cells(j, 5)) Then
If Sheet2.Cells(j, 3).Value > maxTT Then
maxRN = j
**maxTT = Sheet2.Cells(j, 3).Value
End If
End If
Next j
If totalB > totalA Then
Sheet2.Cells(maxRN, 5).Value = "A"
nbA = nbA + 1
Sheet2.Cells(maxRN, 6).Value = nbA
totalA = totalA + maxTT
ElseIf totalB <= totalA Then
Sheet2.Cells(maxRN, 5).Value = "B"
nbB = nbB + 1
Sheet2.Cells(maxRN, 6).Value = nbB
totalB = totalB + maxTT
End If
Next i
End Sub
这可能是什么原因?我已经回到我的电子表格并手动将数据类型设置为我的范围的数字。这仍然没有解决问题。
答案 0 :(得分:2)
原因很可能是您尝试分配Integer
的数据。我尝试了3
,没有遇到任何问题。最大。 VBA中的整数值为 32767 。
我建议将maxTT
变量声明为Long
。
Dim maxTT As Long
可能还有其他问题,但如果没有输入则难以猜测。