martedì 7 ottobre 2014

Scrittura dei turni in forma alternata con riserva di modifica successiva

Ma forse mi conviene alternare, come ho fatto prima, per poi sostituire alcuni turni per far quadrare il conto.

L'algoritmo era questo:
Dim reparti(1) As String
Dim n As Integer
Dim f As Integer

reparti(0) = "CAR"
reparti(1) = "MED"
 
n = Int(Rnd() * 2)
Range("Reparto").Cells(1, 1).Formula = reparti(n)

For k = 2 To Range("Reparto").Rows.Count
    If n = 0 Then
        n = 1
    Else
        n = 0
    End If
    Range("Reparto").Cells(k, 1).Formula = reparti(n)
    If festivo(Range("Reparto").Cells(k, 1).Offset(0, -2).Formula) Then
        If Range("Reparto").Cells(k, 1).Formula = "MED" Then
            Range("Reparto").Cells(k, 1).Formula = "CAR - MED"
        Else
            Range("Reparto").Cells(k, 1).Formula = "MED - CAR"
        End If
        f = f + 1
    End If
Next k
in cui predispongo un array di due elementi stringa, "CAR" e "MED", quindi estraggo casualmente uno dei due e lo metto nella prima cella:
reparti(0) = "CAR"
reparti(1) = "MED"
 
n = Int(Rnd() * 2)
Range("Reparto").Cells(1, 1).Formula = reparti(n)
Quindi alterno la disposizione successiva in questo modo:
For k = 2 To Range("Reparto").Rows.Count
    If n = 0 Then
        n = 1
    Else
        n = 0
    End If
    Range("Reparto").Cells(k, 1).Formula = reparti(n)
Next k
Ossia prendo alternativamente i due elementi dell'array, "CAR" e "MED".

Per i festivi uso questo:
If festivo(Range("Reparto").Cells(k, 1).Offset(0, -2).Formula) Then
        If Range("Reparto").Cells(k, 1).Formula = "MED" Then
            Range("Reparto").Cells(k, 1).Formula = "CAR - MED"
        Else
            Range("Reparto").Cells(k, 1).Formula = "MED - CAR"
        End If
        f = f + 1
    End If
mettendo il giorno a CAR se la notte è MED e viceversa.
In tutto questo, sapendo il numero teorico di turni per CAR e MED, posso inserire un codice che conti i vari CAR e MED in modo da operare poi le sostituzioni nel modo opportuno.
Proviamo...
Private Sub attribuzioneTurni()
    Dim reparti(1) As String
    Dim n As Integer
    Dim f As Integer
    Dim tCar, tMed As Integer

    reparti(0) = "CAR"
    reparti(1) = "MED"
 
     n = Int(Rnd() * 2)
     Range("Reparto").Cells(1, 1).Formula = reparti(n)
    If reparti(n) = "CAR" Then
        tCar = tCar + 1
    Else
        tMed = tMed + 1
    End If

    For k = 2 To Range("Reparto").Rows.Count
        If n = 0 Then
            n = 1
        Else
            n = 0
        End If
        Range("Reparto").Cells(k, 1).Formula = reparti(n)
        If reparti(n) = "CAR" Then
            tCar = tCar + 1
        Else
            tMed = tMed + 1
        End If
    
        If festivo(Range("Reparto").Cells(k, 1).Offset(0, -2).Formula) Then
            If Range("Reparto").Cells(k, 1).Formula = "MED" Then
                Range("Reparto").Cells(k, 1).Formula = "CAR - MED"
                tCar = tCar + 1
            Else
                Range("Reparto").Cells(k, 1).Formula = "MED - CAR"
                tMed = tMed + 1
            End If
            f = f + 1
        End If
    Next k
    Debug.Print "turni card " & tCar
    Debug.Print "turni med " & tMed

End Sub
Ecco: le aggiunte contrassegnate in rosso su giallo sono quelle che ho fatto per contare i turni CAR e i turni MED del mese.
Ho contrassegnato peraltro in bianco su blu una variabile che mi serve per contare il numero dei festivi e aggiungerlo al numero delle celle in modo da avere il totale dei turni disponibili.

Nessun commento:

Posta un commento