Ripetere una routine e memorizzare i risultati



  • Ripetere una routine e memorizzare i risultati
    di daligeno data: 01/05/2017 18:38:24

    Salve a tutti!
    Metto in allegato il file di un mio progetto universitario e vi espongo un piccolo problema.
    Il codice di riferimento è quello del modulo3
    Dovrei fare in modo che l'intera routine Turnaz_Riposo, che al suo interno ne richiama poi delle altre, si ripeta per un tot di volte (ad esempio 20-25) e memorizzando di volta in volta il risultato della cella Z27, in rosso sul foglio excel. Ogni qual volta viene eseguita la routine vorrei che però questi risultati venissero riportati su un altro foglio di lavoro.
    In teoria dovrebbe essere semplice da fare ma mi sono parecchio incartata
    Ringrazio in anticipo chiunque voglia aiutarmi



  • di ges data: 02/05/2017 00:48:31

    Ciao, senza entrare nel merito della macro, mi sono limitato alla tua richiesta, aggiungendo la parte iniziale e finale. 

    Ho aggiunto un foglio chiamandolo "Risultati", nella cella A1 di questo ho scritto somma e ho modificato il codice come di seguito.
     
    'per Foglio3 - con straordinari e Riposo
    Sub Turnaz_Riposo()
    Dim matrix(1 To 22), extract(1 To 22), vt(1 To 8)
    Dim i As Long, j As Long, k As Long, h As Long, w As Long, numcas As Integer
    Dim fabb_so, fabb_rep, fabb_dp, fabb, max
    
    
    '----------------------------------------------- pezzo aggiunto
    Dim ripeti As Integer
    Dim uRow As Integer
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    For ripeti = 1 To 30
    '------------------------------------------------
    
    
    
    Range("B5:W26").ClearContents
        For i = 1 To 22
            matrix(i) = i  ': extract(i) = i
        Next i
    'inserimento turno1 SO
    For i = 2 To 18 Step 3
        For j = 1 To 22
            extract(j) = j
        Next j
        fabb = Cells(37, i)
        For k = 1 To fabb
    dinuovo:
            Randomize
            numcas = Int((22 - 1 + 1) * Rnd + 1)
            For h = 1 To 22 'controllo se già stato estratto
                If extract(numcas) = 0 Then
                    GoTo dinuovo 'altrimenti ne estraggo un altro
                Else
                    extract(numcas) = 0
                    Exit For
                End If
            Next h
            numcas = numcas + 4
            max = Cells(27, i)
            If max < fabb Then
                Cells(numcas, i) = 1
            End If
        Next k
    Next i
    'inserimento turno2 SO+DP+RR
    For i = 3 To 18 Step 3
        For j = 5 To 26
            If Cells(j, i - 1) = 1 Then matrix(j - 4) = 1
            extract(j - 4) = j - 4
        Next j
        fabb = Cells(37, i)
        fabb_so = Cells(31, i)
        For k = 1 To fabb
    ancora:
            Randomize
            numcas = Int((22 - 1 + 1) * Rnd + 1)
            For h = 1 To 22 'controllo se già stato estratto
                If extract(numcas) = 0 Then
                    GoTo ancora 'altrimenti ne estraggo un altro
                Else
                    extract(numcas) = 0
                    Exit For
                End If
            Next h
            numcas = numcas + 4
            max = Cells(27, i)
            If max = 0 Then
                Cells(numcas, i) = 3
            ElseIf max > 0 Then
                Cells(numcas, i) = 1
            End If
            
            If max = fabb_so Then
            Cells(numcas, i) = 4
            
            End If
        Next k
    Next i
    'inserimento turno REP (domenica esclusa)
    For i = 4 To 19 Step 3
        For j = 5 To 26
        If Cells(j, i - 1) = 1 Or Cells(j, i - 2) = 1 Or _
                Cells(j, i - 1) = 3 Then matrix(j - 4) = 1
                'extract(j - 4) = j - 4
        Next j
        fabb = Cells(37, i)
        For k = 1 To fabb
    turno2:
            Randomize
            numcas = Int((22 - 1 + 1) * Rnd + 1)
            For h = 1 To 22 'controllo se già stato estratto
                If extract(numcas) = 0 Then
                    GoTo turno2 'altrimenti ne estraggo un altro
                Else
                    extract(numcas) = 0
                    Exit For
                End If
            Next h
            numcas = numcas + 4
            max = Cells(27, i)
            
            If max < fabb Then
            
                Cells(numcas, i) = 2
                End If
         Next k
    Next i
    'inserimento turnoDOM (domenica)
    For i = 20 To 21
        For j = 5 To 26
            If Cells(j, i - 1) = 1 Or Cells(j, i - 2) = 1 Or _
                Cells(j, i - 1) = 3 Then matrix(j - 4) = 1
                extract(j - 4) = j - 4
        Next j
        fabb = Cells(37, i)
        For k = 1 To fabb
    turnoDOM:
            Randomize
            numcas = Int((22 - 1 + 1) * Rnd + 1)
            For h = 1 To 22 'controllo se già stato estratto
                If extract(numcas) = 0 Then
                    GoTo turnoDOM 'altrimenti ne estraggo un altro
                Else
                    extract(numcas) = 0
                    Exit For
                End If
            Next h
            numcas = numcas + 4
            max = Cells(27, i)
            If max < fabb Then
                Cells(numcas, i) = 2
            End If
        Next k
    Next i
    j = 0
    For i = 5 To 26
        If Cells(i, 5) = "" Then
            j = j + 1: vt(j) = i
        End If
    Next i
    Randomize
    numcas = Int((j - 1 + 1) * Rnd + 1)
    Cells(vt(numcas), 5) = 1
    Application.EnableEvents = True
    Calc_Ore_Operatore
    Range("W5:Y26").ClearContents
    cerca_elemento
    
    
    '--------------------------------------- pezzo aggiunto
    uRow = Sheets("Risultati").Cells(Rows.Count, 1).End(xlUp).Row + 1
    Sheets("Risultati").Cells(uRow, 1) = Sheets("Foglio3").Cells(27, "Z")
    Next ripeti
    Application.ScreenUpdating = True
    '----------------------------------------
    
    
    End Sub



  • di daligeno data: 02/05/2017 01:52:02

    grazie mille ges,

    Ma c'è un modo per vedere ogni risultato ottenuto a quale configurazione appartiene?
    Ad esempio, individuato il minimo valore tra tutte le 30 prove vorrei far ripartire la routine da quella configurazione per trovare un valore ancora minore rispetto al primo di partenza, per poi fermarmi quando non ne trova più.

    Dalila