Login Registrati
Stai vedendo 7 articoli - dal 1 a 7 (di 7 totali)
  • Autore
    Articoli
  • #16988 Score: 0 | Risposta

    ROB62GIA
    Partecipante

      buongiorno

      Ho 57 anni e da poco mi sto dedicndo al VBA di Excel ho scritto una Mcro e volevo chiedere agli esperti del forum se c'è modo di velocizzarla ringrazio sin d'ora quanti con la loro esperienza potranno aiutarmi  - Roberto -

      Sub Elab6()
      
          For W1 = 1 To Sheets("DUE").Range("A7")
      Range("A5") = Sheets("DUE").Range("FA" & W1).Value
      Range("A10") = W1
      
          For W21 = W1 To Sheets("DUE").Range("A7")
      Range("B5") = Sheets("DUE").Range("FA" & W21).Value
      Range("A11") = W21
      
          For W2 = 1 To Sheets("DUE").Range("B7")
      Range("G5") = Sheets("DUE").Range("FB" & W2).Value
      Range("B10") = W2
      
          For W22 = W2 To Sheets("DUE").Range("B7")
      Range("H5") = Sheets("DUE").Range("FB" & W22).Value
      Range("B11") = W22
      
          For W3 = 1 To Sheets("DUE").Range("C7")
      Range("M5") = Sheets("DUE").Range("FC" & W3).Value
      Range("C10") = W3
      
          For W23 = W3 To Sheets("DUE").Range("C7")
      Range("N5") = Sheets("DUE").Range("FC" & W23).Value
      Range("C11") = W23
      
          For W4 = 1 To Sheets("DUE").Range("D7")
      Range("S5") = Sheets("DUE").Range("FD" & W4).Value
      Range("D10") = W4
      
          For W24 = W4 To Sheets("DUE").Range("D7")
      Range("T5") = Sheets("DUE").Range("FD" & W24).Value
      Range("D11") = W24
      
          For W5 = 1 To Sheets("DUE").Range("E7")
      Range("Y5") = Sheets("DUE").Range("FE" & W5).Value
      Range("E10") = W5
          
          For W25 = W5 To Sheets("DUE").Range("E7")
      Range("Z5") = Sheets("DUE").Range("FE" & W25).Value
      Range("E11") = W25
           
          For W6 = 1 To Sheets("DUE").Range("F7")
      Range("AE5") = Sheets("DUE").Range("FF" & W6).Value
      Range("F10") = W6
      
          For W26 = W6 To Sheets("DUE").Range("F7")
      Range("AF5") = Sheets("DUE").Range("FF" & W26).Value
      Range("F11") = W26
      
          For W7 = 1 To Sheets("DUE").Range("G7")
      Range("AK5") = Sheets("DUE").Range("FG" & W7).Value
      Range("G10") = W7
          
          For W27 = W7 To Sheets("DUE").Range("G7")
      Range("AL5") = Sheets("DUE").Range("FG" & W27).Value
      Range("G11") = W27
          
          For W8 = 1 To Sheets("DUE").Range("H7")
      Range("AQ5") = Sheets("DUE").Range("FH" & W8).Value
      Range("H10") = W8
      
          For W28 = W8 To Sheets("DUE").Range("H7")
      Range("AR5") = Sheets("DUE").Range("FH" & W28).Value
      Range("H11") = W28
      
          For W9 = 1 To Sheets("DUE").Range("I7")
      Range("AW5") = Sheets("DUE").Range("FI" & W9).Value
      Range("I10") = W9
      
          For W29 = W9 To Sheets("DUE").Range("I7")
      Range("AX5") = Sheets("DUE").Range("FI" & W29).Value
      Range("I11") = W29
      
          For W10 = 1 To Sheets("DUE").Range("J7")
      Range("BC5") = Sheets("DUE").Range("FJ" & W10).Value
      Range("J10") = W10
      
          For W30 = W10 To Sheets("DUE").Range("J7")
      Range("BD5") = Sheets("DUE").Range("FJ" & W30).Value
      Range("J11") = W30
      
          For W11 = 1 To Sheets("DUE").Range("K7")
      Range("BI5") = Sheets("DUE").Range("FK" & W11).Value
      Range("K10") = W11
      
          For W31 = W11 To Sheets("DUE").Range("K7")
      Range("BJ5") = Sheets("DUE").Range("FK" & W31).Value
      Range("K11") = W31
      
          For W12 = 1 To Sheets("DUE").Range("L7")
      Range("BO5") = Sheets("DUE").Range("FL" & W12).Value
      Range("L10") = W12
      
          For W32 = W12 To Sheets("DUE").Range("L7")
      Range("BP5") = Sheets("DUE").Range("FL" & W32).Value
      Range("L11") = W32
      
          For W13 = 1 To Sheets("DUE").Range("M7")
      Range("BU5") = Sheets("DUE").Range("FM" & W13).Value
      Range("M10") = W13
      
          For W33 = W13 To Sheets("DUE").Range("M7")
      Range("BV5") = Sheets("DUE").Range("FM" & W33).Value
      Range("M11") = W33
      
          For W14 = 1 To Sheets("DUE").Range("N7")
      Range("CA5") = Sheets("DUE").Range("FN" & W14).Value
      Range("N10") = W14
      
          For W34 = W14 To Sheets("DUE").Range("N7")
      Range("CB5") = Sheets("DUE").Range("FN" & W34).Value
      Range("N11") = W34
      
          For W15 = 1 To Sheets("DUE").Range("O7")
      Range("CG5") = Sheets("DUE").Range("FO" & W15).Value
      Range("O10") = W15
      
          For W35 = W15 To Sheets("DUE").Range("O7")
      Range("CH5") = Sheets("DUE").Range("FO" & W35).Value
      Range("O11") = W35
      
          For W16 = 1 To Sheets("DUE").Range("P7")
      Range("CM5") = Sheets("DUE").Range("FP" & W16).Value
      Range("P10") = W16
      
          For W36 = W16 To Sheets("DUE").Range("P7")
      Range("CN5") = Sheets("DUE").Range("FP" & W36).Value
      Range("P11") = W36
      
          For W17 = 1 To Sheets("DUE").Range("Q7")
      Range("CS5") = Sheets("DUE").Range("FQ" & W17).Value
      Range("Q10") = W17
      
          For W37 = W17 To Sheets("DUE").Range("Q7")
      Range("CT5") = Sheets("DUE").Range("FQ" & W37).Value
      Range("Q11") = W37
      
          For W18 = 1 To Sheets("DUE").Range("R7")
      Range("CY5") = Sheets("DUE").Range("FR" & W18).Value
      Range("R10") = W18
      
          For W38 = W18 To Sheets("DUE").Range("R7")
      Range("CZ5") = Sheets("DUE").Range("FR" & W38).Value
      Range("R11") = W38
      
      L58:
      Sheets("DUE").Range("CZ5").ClearContents
          Next W38
      L38:
      Sheets("DUE").Range("CY5").ClearContents
          Next W18
      L57:
      Sheets("DUE").Range("CT5,CZ5,R11").ClearContents
          Next W37
      L37:
      Sheets("DUE").Range("CS5,CY5,R10").ClearContents
          Next W17
      L56:
      Sheets("DUE").Range("CN5,CT5,CZ5,Q11:R11").ClearContents
          Next W36
      L36:
      Sheets("DUE").Range("CM5,CS5,CY5,Q10:R10").ClearContents
          Next W16
      L55:
      Sheets("DUE").Range("CH5,CN5,CT5,CZ5,P11:R11").ClearContents
          Next W35
      L35:
      Sheets("DUE").Range("CG5,CM5,CS5,CY5,P10:R10").ClearContents
          Next W15
      L54:
      Sheets("DUE").Range("CB5,CH5,CN5,CT5,CZ5,O11:R11").ClearContents
          Next W34
      L34:
      Sheets("DUE").Range("CA5,CG5,CM5,CS5,CY5,O10:R10").ClearContents
          Next W14
      L53:
      Sheets("DUE").Range("BV5,CB5,CH5,CN5,CT5,CZ5,N11:R11").ClearContents
          Next W33
      L33:
      Sheets("DUE").Range("BU5,CA5,CG5,CM5,CS5,CY5,N10:R10").ClearContents
          Next W13
      L52:
      Sheets("DUE").Range("BP5,BV5,CB5,CH5,CN5,CT5,CZ5,M11:R11").ClearContents
          Next W32
      L32:
      Sheets("DUE").Range("BO5,BU5,CA5,CG5,CM5,CS5,CY5,M10:R10").ClearContents
          Next W12
      L51:
      Sheets("DUE").Range("BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,L11:R11").ClearContents
          Next W31
      L31:
      Sheets("DUE").Range("BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,L10:R10").ClearContents
          Next W11
      L50:
      Sheets("DUE").Range("BD5,BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,K11:R11").ClearContents
          Next W30
      L30:
      Sheets("DUE").Range("BC5,BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,K10:R10").ClearContents
          Next W10
      L49:
      Sheets("DUE").Range("AX5,BD5,BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,J11:R11").ClearContents
          Next W29
      L29:
      Sheets("DUE").Range("AW5,BC5,BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,J10:R10").ClearContents
          Next W9
      L48:
      Sheets("DUE").Range("AR5,AX5,BD5,BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,I11:R11").ClearContents
          Next W28
      L28:
      Sheets("DUE").Range("AQ5,AW5,BC5,BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,I10:R10").ClearContents
          Next W8
      L47:
      Sheets("DUE").Range("AL5,AR5,AX5,BD5,BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,H11:R11").ClearContents
          Next W27
      L27:
      Sheets("DUE").Range("AK5,AQ5,AW5,BC5,BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,H10:R10").ClearContents
          Next W7
      L46:
      Sheets("DUE").Range("AF5,AL5,AR5,AX5,BD5,BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,G11:R11").ClearContents
          Next W26
      L26:
      Sheets("DUE").Range("AE5,AK5,AQ5,AW5,BC5,BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,G10:R10").ClearContents
          Next W6
      L45:
      Sheets("DUE").Range("Z5,AF5,AL5,AR5,AX5,BD5,BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,F11:R11").ClearContents
          Next W25
      L25:
      Sheets("DUE").Range("Y5,AE5,AK5,AQ5,AW5,BC5,BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,F10:R10").ClearContents
          Next W5
      L44:
      Sheets("DUE").Range("T5,Z5,AF5,AL5,AR5,AX5,BD5,BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,E11:R11").ClearContents
          Next W24
      L24:
      Sheets("DUE").Range("S5,Y5,AE5,AK5,AQ5,AW5,BC5,BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,E10:R10").ClearContents
          Next W4
      L43:
      Sheets("DUE").Range("N5,T5,Z5,AF5,AL5,AR5,AX5,BD5,BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,D11:R11").ClearContents
          Next W23
      L23:
      Sheets("DUE").Range("M5,S5,Y5,AE5,AK5,AQ5,AW5,BC5,BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,D10:R10").ClearContents
          Next W3
      L42:
      Sheets("DUE").Range("H5,N5,T5,Z5,AF5,AL5,AR5,AX5,BD5,BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,C11:R11").ClearContents
          Next W22
      L22:
      Sheets("DUE").Range("G5,M5,S5,Y5,AE5,AK5,AQ5,AW5,BC5,BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,C10:R10").ClearContents
          Next W2
      L41:
      Sheets("DUE").Range("B5,H5,N5,T5,Z5,AF5,AL5,AR5,AX5,BD5,BJ5,BP5,BV5,CB5,CH5,CN5,CT5,CZ5,B11:R11").ClearContents
          Next W21
      
      L21:
      Sheets("DUE").Range("A5,G5,M5,S5,Y5,AE5,AK5,AQ5,AW5,BC5,BI5,BO5,BU5,CA5,CG5,CM5,CS5,CY5,B10:R10").ClearContents
          Next W1
          
      End Sub`
      #16992 Score: 0 | Risposta

      vecchio frac
      Senior Moderator
        245 pts

        Tutti questi For innestati sono molto pesanti e probabilmente un'ottimizzazione si può pensare, ma se ci descrivi, almeno per sommi capi, lo scenario, il problema e l'obiettivo forse si può pensare a una soluzione più mirata. L'optimum sarebbe avere anche un file con dati di esempio per fare il test "sul campo".

        #16993 Score: 0 | Risposta

        ROB62GIA
        Partecipante

          Allegato File con macro completa

          Allegati:
          You must be logged in to view attached files.
          #16995 Score: 0 | Risposta

          vecchio frac
          Senior Moderator
            245 pts

            La procedura è più complessa di quanto prospettato 🙂

            Andando con ordine:

            - metti sempre la direttiva Option Explicit in testa ai moduli, e quindi

            - dichiara sempre le variabili e il loro tipo;

            - indenta sempre correttamente il codice così i blocchi sono più leggibili (suggerisco, a te ma anche a chiunque legga questo post, questo sito: http://rubberduckvba.com/indentation)

            - in generale, sono ammessi range multipli (per ottenere istruzioni più compatte del tipo: Range("A1,B5,C10").ClearContents)

            Ora devi aver ela pazienza di lasciarmi esaminare il file allegato e studiare il codice perchè non hai fornito alcuna spiegazione sul funzionamento (la richiesta è di "velocizzare" ma sarebbe bello capire cosa fa il codice).

            #16996 Score: 0 | Risposta

            ROB62GIA
            Partecipante

              ti ringrazio per l'attenzione
              non è molto semplice da spiegare
              ma ci provo
              partendo da 18 terzine devo inserire i numeri mancanti
              che sono nelle colonne (GH - GY)
              nelle colonne GB-GF avviene il controllo che verifica che
              nessuno di questi numeri sia già presente nelle terzine date
              e in ogni coppia di numeri che i cicli For producono
              dopo questo controllo, i numeri vengono copiati nelle colonne (FA-FR)
              colonne che conterranno i numeri per i cicli For.
              i cicli for inseriscono i numeri nelle celle (A5/B5 - G5/H5 - M5/N5 ecc)
              per un secondo controllo che vien dettato dalle formule nelle righe (18 - 35)

              #17000 Score: 0 | Risposta

              vecchio frac
              Senior Moderator
                245 pts

                Se tutto l'ambaradan ti funziona, per quanto magari lento, non lo toccherei più di tanto, perchè si rischi di mettere fuori uso qualche riferimento che invece funziona. Piccoli accorgimenti non funzionali alla fine non influirebbero sulle prestazioni. Ho avuto difficoltà anche con Activeworkbook.WorkSort nella routine di ordinamento (che versione di Excel hai?)

                #17004 Score: 0 | Risposta

                ROB62GIA
                Partecipante

                  ho excel 2010

                Login Registrati
                Stai vedendo 7 articoli - dal 1 a 7 (di 7 totali)
                Rispondi a: Velocizzare cicli For
                Gli allegati sono permessi solo ad utenti REGISTRATI
                Le tue informazioni: