Non ho fatto molte prove perchè sono di fretta cmq prova con questa macro.Sub ConteggioMarche()
ConteggioMarche = 0
RigaPartenza = 10
ColonnaRisultato = 8
RigaRisultato = RigaPartenza
SommaCorrente = 0
ValoreDaDividere = InputBox("Immettere il valore da dividere", "Valore")
Cells(RigaRisultato - 3, ColonnaRisultato + 1) = ValoreDaDividere
Cells(RigaRisultato - 3, ColonnaRisultato) = "Valore"
Cells(RigaRisultato - 1, ColonnaRisultato) = "Nro"
Cells(RigaRisultato - 1, ColonnaRisultato + 1) = "Importo"
For CellaValoreMarca = 7 To 31
ValoreMarcaCorrente = Cells(CellaValoreMarca, 17).Value
If ValoreDaDividere > ValoreMarcaCorrente Then
NroMarche = Int(ValoreDaDividere / Cells(CellaValoreMarca, 17).Value)
ConteggioMarche = ConteggioMarche + NroMarche
If ConteggioMarche > 9 And NroMarche = 1 Then
GoTo fine
ElseIf ConteggioMarche > 9 Then
'ancora da fare...
MsgBox ("da fare")
GoTo fine
End If
Cells(RigaRisultato, ColonnaRisultato) = NroMarche
Cells(RigaRisultato, ColonnaRisultato + 1) = ValoreMarcaCorrente
RigaRisultato = RigaRisultato + 1
ValoreDaDividere = ValoreDaDividere - (NroMarche * ValoreMarcaCorrente)
SommaCorrente = SommaCorrente + NroMarche * ValoreMarcaCorrente
End If
Next
fine:
Cells(RigaRisultato + 3, ColonnaRisultato - 1) = ConteggioMarche
Cells(RigaRisultato + 2, ColonnaRisultato - 1) = "Nro Marche"
Cells(RigaRisultato + 2, ColonnaRisultato + 1) = SommaCorrente
Cells(RigaRisultato + 2, ColonnaRisultato + 2) = "Totale"
If ValoreDaDividere > 0 Then Cells(RigaRisultato + 3, ColonnaRisultato + 1) = ValoreDaDividere
Cells(RigaRisultato + 3, ColonnaRisultato + 2) = "Resto"
End SubCiao
Supponi di fare una cambiale di 100 euro, la tua macro mi propone:
Valore 100,00
Nro Importo
1 60,00
1 30,00
1 7,75
1 1,55
1 0,62
1 0,05
totale 99,97
resto 0,03
invece il calcolo ottimale è:
1 60,00
1 30,00
1 6,00
1 2,40
1 1,55
1 0,05
totale 100,00
resto 0,00
Secondo me l'unica è valutare tutte le permutazioni possibili ( prima pensavo alle combinazioni, ma mi sbagliavo):
1 25
2 600
3 13.800
4 303.600
5 6.375.600
6 127.512.000
7 2.422.728.000
8 43.609.104.000
9 741.354.768.000
10 11.861.676.288.000
totale 12.649.197.093.625
Questo codice prova tutte le combinazioni di 1,2,3,4,5,6,7,8,9,10 marche con valore da 1 a 3, ma è troppo pesante, figuriamoci con 25 valori.
Si può snellire e velocizzare?
Sub CONTEGGIO()
Dim Inizio, Inizio2, Inizio3, Inizio4, Inizio5, Inizio6, Inizio7, Inizio8, Inizio9, Inizio10
Inizio = 1
Inizio2 = 1
Inizio3 = 1
Inizio4 = 1
Inizio5 = 1
Inizio6 = 1
Inizio7 = 1
Inizio8 = 1
Inizio9 = 1
Inizio10 = 1
For Each Cell In Range("A1:A3")
Cells(1, 2) = Cell
Next
For Each Cell In Range("A1:A3")
For Each Cell2 In Range(Cells(Inizio2, 1), Cells(3, 1))
Cells(1, 2) = Cell
Cells(1, 3) = Cell2
Next
Inizio2 = Inizio2 + 1
Next
Inizio2 = 1
Inizio3 = 1
For Each Cell In Range("A1:A3")
For Each Cell2 In Range(Cells(Inizio2, 1), Cells(3, 1))
For Each Cell3 In Range(Cells(Inizio3, 1), Cells(3, 1))
Cells(1, 2) = Cell
Cells(1, 3) = Cell2
Cells(1, 4) = Cell3
Next
Next
Inizio2 = Inizio2 + 1
Inizio3 = Inizio3 + 1
Next
Inizio2 = 1
Inizio3 = 1
Inizio4 = 1
For Each Cell In Range("A1:A3")
For Each Cell2 In Range(Cells(Inizio2, 1), Cells(3, 1))
For Each Cell3 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell4 In Range(Cells(Inizio3, 1), Cells(3, 1))
Cells(1, 2) = Cell
Cells(1, 3) = Cell2
Cells(1, 4) = Cell3
Cells(1, 5) = Cell4
Next
Next
Next
Inizio2 = Inizio2 + 1
Inizio3 = Inizio3 + 1
Inizio4 = Inizio4 + 1
Next
Inizio2 = 1
Inizio3 = 1
Inizio4 = 1
Inizio5 = 1
For Each Cell In Range("A1:A3")
For Each Cell2 In Range(Cells(Inizio2, 1), Cells(3, 1))
For Each Cell3 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell4 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell5 In Range(Cells(Inizio4, 1), Cells(3, 1))
Cells(1, 2) = Cell
Cells(1, 3) = Cell2
Cells(1, 4) = Cell3
Cells(1, 5) = Cell4
Cells(1, 6) = Cell5
Next
Next
Next
Next
Inizio2 = Inizio2 + 1
Inizio3 = Inizio3 + 1
Inizio4 = Inizio4 + 1
Inizio5 = Inizio5 + 1
Next
Inizio2 = 1
Inizio3 = 1
Inizio4 = 1
Inizio5 = 1
Inizio6 = 1
For Each Cell In Range("A1:A3")
For Each Cell2 In Range(Cells(Inizio2, 1), Cells(3, 1))
For Each Cell3 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell4 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell5 In Range(Cells(Inizio4, 1), Cells(3, 1))
For Each Cell6 In Range(Cells(Inizio5, 1), Cells(3, 1))
Cells(1, 2) = Cell
Cells(1, 3) = Cell2
Cells(1, 4) = Cell3
Cells(1, 5) = Cell4
Cells(1, 6) = Cell5
Cells(1, 7) = Cell6
Next
Next
Next
Next
Next
Inizio2 = Inizio2 + 1
Inizio3 = Inizio3 + 1
Inizio4 = Inizio4 + 1
Inizio5 = Inizio5 + 1
Inizio6 = Inizio6 + 1
Next
Inizio2 = 1
Inizio3 = 1
Inizio4 = 1
Inizio5 = 1
Inizio6 = 1
Inizio7 = 1
For Each Cell In Range("A1:A3")
For Each Cell2 In Range(Cells(Inizio2, 1), Cells(3, 1))
For Each Cell3 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell4 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell5 In Range(Cells(Inizio4, 1), Cells(3, 1))
For Each Cell6 In Range(Cells(Inizio5, 1), Cells(3, 1))
For Each Cell7 In Range(Cells(Inizio6, 1), Cells(3, 1))
Cells(1, 2) = Cell
Cells(1, 3) = Cell2
Cells(1, 4) = Cell3
Cells(1, 5) = Cell4
Cells(1, 6) = Cell5
Cells(1, 7) = Cell6
Cells(1, 8) = Cell7
Next
Next
Next
Next
Next
Next
Inizio2 = Inizio2 + 1
Inizio3 = Inizio3 + 1
Inizio4 = Inizio4 + 1
Inizio5 = Inizio5 + 1
Inizio6 = Inizio6 + 1
Inizio7 = Inizio7 + 1
Next
Inizio2 = 1
Inizio3 = 1
Inizio4 = 1
Inizio5 = 1
Inizio6 = 1
Inizio7 = 1
Inizio8 = 1
For Each Cell In Range("A1:A3")
For Each Cell2 In Range(Cells(Inizio2, 1), Cells(3, 1))
For Each Cell3 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell4 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell5 In Range(Cells(Inizio4, 1), Cells(3, 1))
For Each Cell6 In Range(Cells(Inizio5, 1), Cells(3, 1))
For Each Cell7 In Range(Cells(Inizio6, 1), Cells(3, 1))
For Each Cell8 In Range(Cells(Inizio7, 1), Cells(3, 1))
Cells(1, 2) = Cell
Cells(1, 3) = Cell2
Cells(1, 4) = Cell3
Cells(1, 5) = Cell4
Cells(1, 6) = Cell5
Cells(1, 7) = Cell6
Cells(1, 8) = Cell7
Cells(1, 9) = Cell8
Next
Next
Next
Next
Next
Next
Next
Inizio2 = Inizio2 + 1
Inizio3 = Inizio3 + 1
Inizio4 = Inizio4 + 1
Inizio5 = Inizio5 + 1
Inizio6 = Inizio6 + 1
Inizio7 = Inizio7 + 1
Inizio8 = Inizio8 + 1
Next
Inizio2 = 1
Inizio3 = 1
Inizio4 = 1
Inizio5 = 1
Inizio6 = 1
Inizio7 = 1
Inizio8 = 1
Inizio9 = 1
For Each Cell In Range("A1:A3")
For Each Cell2 In Range(Cells(Inizio2, 1), Cells(3, 1))
For Each Cell3 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell4 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell5 In Range(Cells(Inizio4, 1), Cells(3, 1))
For Each Cell6 In Range(Cells(Inizio5, 1), Cells(3, 1))
For Each Cell7 In Range(Cells(Inizio6, 1), Cells(3, 1))
For Each Cell8 In Range(Cells(Inizio7, 1), Cells(3, 1))
For Each Cell9 In Range(Cells(Inizio8, 1), Cells(3, 1))
Cells(1, 2) = Cell
Cells(1, 3) = Cell2
Cells(1, 4) = Cell3
Cells(1, 5) = Cell4
Cells(1, 6) = Cell5
Cells(1, 7) = Cell6
Cells(1, 8) = Cell7
Cells(1, 9) = Cell8
Cells(1, 10) = Cell9
Next
Next
Next
Next
Next
Next
Next
Next
Inizio2 = Inizio2 + 1
Inizio3 = Inizio3 + 1
Inizio4 = Inizio4 + 1
Inizio5 = Inizio5 + 1
Inizio6 = Inizio6 + 1
Inizio7 = Inizio7 + 1
Inizio8 = Inizio8 + 1
Inizio9 = Inizio9 + 1
Next
Inizio2 = 1
Inizio3 = 1
Inizio4 = 1
Inizio5 = 1
Inizio6 = 1
Inizio7 = 1
Inizio8 = 1
Inizio9 = 1
Inizio10 = 1
For Each Cell In Range("A1:A3")
For Each Cell2 In Range(Cells(Inizio2, 1), Cells(3, 1))
For Each Cell3 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell4 In Range(Cells(Inizio3, 1), Cells(3, 1))
For Each Cell5 In Range(Cells(Inizio4, 1), Cells(3, 1))
For Each Cell6 In Range(Cells(Inizio5, 1), Cells(3, 1))
For Each Cell7 In Range(Cells(Inizio6, 1), Cells(3, 1))
For Each Cell8 In Range(Cells(Inizio7, 1), Cells(3, 1))
For Each Cell9 In Range(Cells(Inizio8, 1), Cells(3, 1))
For Each Cell10 In Range(Cells(Inizio9, 1), Cells(3, 1))
Cells(1, 2) = Cell
Cells(1, 3) = Cell2
Cells(1, 4) = Cell3
Cells(1, 5) = Cell4
Cells(1, 6) = Cell5
Cells(1, 7) = Cell6
Cells(1, 8) = Cell7
Cells(1, 9) = Cell8
Cells(1, 10) = Cell9
Cells(1, 11) = Cell10
Next
Next
Next
Next
Next
Next
Next
Next
Next
Inizio2 = Inizio2 + 1
Inizio3 = Inizio3 + 1
Inizio4 = Inizio4 + 1
Inizio5 = Inizio5 + 1
Inizio6 = Inizio6 + 1
Inizio7 = Inizio7 + 1
Inizio8 = Inizio8 + 1
Inizio9 = Inizio9 + 1
Inizio10 = Inizio10 + 1
Next
End Sub
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.