Utilizzo di Excel (2010) VBA, sto cercando di copiare (passare) una gamma costante di celle (i cui valori ricalcolare) in una matrix. Poi sto cercando di passare quella matrix a una nuova gamma di celle, direttamente sotto di essa. Dopo averlo fatto, desidero copiare (passare) i nuovi valori della gamma costante nell'arrays e passare questi nuovi valori ad un intervallo immediatamente inferiore a quello precedentemente passato.
So che questo codice è atroce (io sono nuovo alle matrici in VBA).
Sub ARRAYER() Dim anARRAY(5) As Variant Number_of_Sims = 10 For i = 1 To Number_of_Sims anARRAY = Range("C4:G4") Range("C4").Select ActiveCell.Offset(Number_of_Sims, 0).Select ActiveCell = anARRAY Range("C4").Select Next End Sub
Sono sicuro di apprezzare il tuo aiuto!
Grazie.
Rispettosamente,
Jonathan
Lei è leggermente su alcune cose qui, quindi speriamo che il seguente aiuta.
In primo luogo, non è necessario select le aree per accedere alle loro properties;, è sufficiente specificare il proprio indirizzo, ecc. In secondo luogo, a less che non si manipolino i valori all'interno dell'intervallo, non è necessario impostarli in una variante. Se si desidera modificare i valori, è ansible escludere i limiti dell'arrays come verrà impostato quando si definisce l'intervallo.
E 'anche una buona pratica usare l' Option Explicit
nella parte superiore dei moduli per forzare la dichiarazione variabile.
Quello che segue farà quello che stai cercando:
Sub ARRAYER() Dim Number_of_Sims As Integer, i As Integer Number_of_Sims = 10 For i = 1 To Number_of_Sims 'Do your calculation here to update C4 to G4 Range(Cells(4 + i, "C"), Cells(4 + i, "G")).Value = Range("C4:G4").Value Next End Sub
Se si desidera modificare i valori all'interno dell'arrays, eseguire questa operazione:
Sub ARRAYER() Dim Number_of_Sims As Integer, i As Integer Dim anARRAY as Variant Number_of_Sims = 10 For i = 1 To Number_of_Sims 'Do your calculation here to update C4 to G4 anARRAY= Range("C4:G4").Value 'You can loop through the arrays and manipulate it here Range(Cells(4 + i, "C"), Cells(4 + i, "G")).Value = anARRAY Next End Sub
Non c'è bisogno di arrays. Basta usare qualcosa di simile:
Sub ARRAYER() Dim Rng As Range Dim Number_of_Sims As Long Dim i As Long Number_of_Sims = 10 Set Rng = Range("C4:G4") For i = 1 To Number_of_Sims Rng.Offset(i, 0).Value = Rng.Value Worksheets("Sheetname").Calculate 'replacing Sheetname with name of your sheet Next End Sub
Poiché state copiando i medesimi dati in tutte le righe, in realtà non è necessario disporre di un ciclo. Prova questo:
Sub ARRAYER() Dim Number_of_Sims As Long Dim rng As Range Application.Calculation = xlCalculationManual Application.ScreenUpdating = False Number_of_Sims = 100000 Set rng = Range("C4:G4") rng.Offset(1, 0).Resize(Number_of_Sims) = rng.Value Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub
Quando ho provato il tuo codice ho avuto Errore quando ho voluto riempire l'Array.
puoi provare a riempire l'Array come questo.
Sub Testing_Data() Dim k As Long, S2 As Worksheet, VArray Application.ScreenUpdating = False Set S2 = ThisWorkbook.Sheets("Sheet1") With S2 VArray = .Range("A1:A" & .Cells(Rows.Count, "A").End(xlUp).Row) End With For k = 2 To UBound(VArray, 1) S2.Cells(k, "B") = VArray(k, 1) / 100 S2.Cells(k, "C") = VArray(k, 1) * S2.Cells(k, "B") Next End Sub