Hello,
The last week I've been programming on a Excel-file that should calculate the optimal combination of persons and locations. I've made a table with 5 rows and 5 coloms. In the cells is a score filled in what gives an indication of the knowlegde of the person on that location (considering the activities he has to do there). I'm searching for the optimal combination, where each person has to have 1 location. To find this, I've made a calculation (zie VBA). For this small table it's simple to program, but I would like to extand this table to a 150 by 150 table (or even bigger). This means a lot of calculations and a lot of programming if I proceed this method. I'm searching for an array that ensures me that the loop isn't getting to big. Thanks for your effort!
The VBA code for the 5 by 5 table:
The VBA-code I've now (but is not working) is:
The last week I've been programming on a Excel-file that should calculate the optimal combination of persons and locations. I've made a table with 5 rows and 5 coloms. In the cells is a score filled in what gives an indication of the knowlegde of the person on that location (considering the activities he has to do there). I'm searching for the optimal combination, where each person has to have 1 location. To find this, I've made a calculation (zie VBA). For this small table it's simple to program, but I would like to extand this table to a 150 by 150 table (or even bigger). This means a lot of calculations and a lot of programming if I proceed this method. I'm searching for an array that ensures me that the loop isn't getting to big. Thanks for your effort!
The VBA code for the 5 by 5 table:
Code:
‘----------------------------------------------------------------------------------------
Private Sub CommandButton1_Click()
Dim rij%, rij1%, rij2%, rij3%, rij4%, rij5%, rijt%
Dim wp1%, wp2%, wp3%, wp4%, wp5%
Dim optie%, optie1%, optie2%
'I'm Dutch, so some variable's are in Dutch
'rij = row
'optie = option
'wp = werkplek = "location where someone works"
rij = 3
rij1 = 3
rij2 = 3
rij3 = 3
rij4 = 3
rij5 = 3
optie = 0
optie1 = 0
optie2 = 0
While rij1 < 8
While rij2 < 8
If rij2 <> rij1 Then
While rij3 < 8
If rij3 <> rij1 Then
If rij3 <> rij2 Then
While rij4 < 8
If rij4 <> rij1 Then
If rij4 <> rij2 Then
If rij4 <> rij3 Then
While rij5 < 8
If rij5 <> rij1 Then
If rij5 <> rij2 Then
If rij5 <> rij3 Then
If rij5 <> rij4 Then
wp1 = Sheets("Sheet1").Cells(rij1, 2)
wp2 = Sheets("Sheet1").Cells(rij2, 3)
wp3 = Sheets("Sheet1").Cells(rij3, 4)
wp4 = Sheets("Sheet1").Cells(rij4, 5)
wp5 = Sheets("Sheet1").Cells(rij5, 6)
optie = wp1 * wp2 * wp3 * wp4 * wp5
optie1 = optie
If optie1 > optie2 Then
optie2 = optie1
Sheets("Sheet2").Select
Sheets("Sheet2").Range("A3:F15000").Delete
rij = 3
Sheets("Sheet2").Cells(rij, 1) = Sheets("Sheet1").Cells(rij1, 1)
Sheets("Sheet2").Cells(rij, 2) = Sheets("Sheet1").Cells(rij2, 1)
Sheets("Sheet2").Cells(rij, 3) = Sheets("Sheet1").Cells(rij3, 1)
Sheets("Sheet2").Cells(rij, 4) = Sheets("Sheet1").Cells(rij4, 1)
Sheets("Sheet2").Cells(rij, 5) = Sheets("Sheet1").Cells(rij5, 1)
Sheets("Sheet2").Cells(rij, 6) = optie2
Sheets("Sheet2").Cells(rij, 7) = wp1
Sheets("Sheet2").Cells(rij, 8) = wp2
Sheets("Sheet2").Cells(rij, 9) = wp3
Sheets("Sheet2").Cells(rij, 10) = wp4
Sheets("Sheet2").Cells(rij, 11) = wp5
Else
If optie1 = optie2 Then
rij = rij + 1
Sheets("Sheet2").Cells(rij, 1) = Sheets("Sheet1").Cells(rij1, 1)
Sheets("Sheet2").Cells(rij, 2) = Sheets("Sheet1").Cells(rij2, 1)
Sheets("Sheet2").Cells(rij, 3) = Sheets("Sheet1").Cells(rij3, 1)
Sheets("Sheet2").Cells(rij, 4) = Sheets("Sheet1").Cells(rij4, 1)
Sheets("Sheet2").Cells(rij, 5) = Sheets("Sheet1").Cells(rij5, 1)
Sheets("Sheet2").Cells(rij, 6) = optie2
End If
End If
End If
End If
End If
End If
rij5 = rij5 + 1
Wend
rij5 = 3
End If
End If
End If
rij4 = rij4 + 1
Wend
rij4 = 3
End If
End If
rij3 = rij3 + 1
Wend
rij3 = 3
End If
rij2 = rij2 + 1
Wend
rij2 = 3
rij1 = rij1 + 1
Wend
End Sub
‘--------------------------------------------------------------------------------------
Code:
'--------------
For i = 1 To 5
While rij(i) < 8
For j = 1 To i
If rij(i) <> rij(j) Then
wp1 = Sheets("Sheet1").Cells(rij1, 2)
wp2 = Sheets("Sheet1").Cells(rij2, 3)
wp3 = Sheets("Sheet1").Cells(rij3, 4)
wp4 = Sheets("Sheet1").Cells(rij4, 5)
wp5 = Sheets("Sheet1").Cells(rij5, 6)
optie = wp1 * wp2 * wp3 * wp4 * wp5
optie1 = optie
If optie1 > optie2 Then
optie2 = optie1
Sheets("Sheet2").Select
Sheets("Sheet2").Range("A3:F15000").Delete
rij = 3
Sheets("Sheet2").Cells(rij, 1) = Sheets("Sheet1").Cells(rij1, 1)
Sheets("Sheet2").Cells(rij, 2) = Sheets("Sheet1").Cells(rij2, 1)
Sheets("Sheet2").Cells(rij, 3) = Sheets("Sheet1").Cells(rij3, 1)
Sheets("Sheet2").Cells(rij, 4) = Sheets("Sheet1").Cells(rij4, 1)
Sheets("Sheet2").Cells(rij, 5) = Sheets("Sheet1").Cells(rij5, 1)
Sheets("Sheet2").Cells(rij, 6) = optie2
Sheets("Sheet2").Cells(rij, 7) = wp1
Sheets("Sheet2").Cells(rij, 8) = wp2
Sheets("Sheet2").Cells(rij, 9) = wp3
Sheets("Sheet2").Cells(rij, 10) = wp4
Sheets("Sheet2").Cells(rij, 11) = wp5
Else
If optie1 = optie2 Then
rij = rij + 1
Sheets("Sheet2").Cells(rij, 1) = Sheets("Sheet1").Cells(rij1, 1)
Sheets("Sheet2").Cells(rij, 2) = Sheets("Sheet1").Cells(rij2, 1)
Sheets("Sheet2").Cells(rij, 3) = Sheets("Sheet1").Cells(rij3, 1)
Sheets("Sheet2").Cells(rij, 4) = Sheets("Sheet1").Cells(rij4, 1)
Sheets("Sheet2").Cells(rij, 5) = Sheets("Sheet1").Cells(rij5, 1)
Sheets("Sheet2").Cells(rij, 6) = optie2
End If
End If
End If
Next j
Wend
Next i
'-----------------