Reputation: 21
I have created a macro file to run a greedy algorithm, that is a heuristic process of assigning the work with the greatest value with to the idle server. The macro should assign 26 works to two workstations running the greedy algorithm.
At first I must determine if the task was assigned and then I must determine which unassigned task has the greatest task time and move on. This should be a pretty straightforward code but I have trouble reading the task times from the spreadsheet which range from (C2:C27). I have written the following code but I get a Run Time Error '13': Type mismatch
at the line I have marked with two **:
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
What might be the reason for this? I have went back to my spreadsheet and manually set the data types as number for my range. This still has not solved the problem.
Upvotes: 0
Views: 79
Reputation: 626825
The reason is most probably with the data you are trying to assign an Integer
with. I tried with 3
, and got no issues. Max. Integer value in VBA is 32767.
I'd suggest to declare the maxTT
variable as Long
.
Dim maxTT As Long
There may be other issues, but without the input it is difficult to guess.
Upvotes: 2