2017-09-26 4 views
0

Ich arbeite an einer Liste und alle Berechnungen auf VBA, aber wenn ich meine Liste in den vordefinierten Bereich schreiben möchte, bekomme ich nichts. Das Folgende ist ein Beispiel für den Code, den ich verwende. Ich poste nicht den tatsächlichen Code, weil es lang ist, aber dieses Beispiel hat das gleiche Problem.Schreiben von einem Array in einen Bereich

Option Explicit 

Sub readArray() 
Dim CoGrade() As Variant 
Dim LastRow As Integer 
Dim NPSeQuedan() As Variant 
Dim SeQuedanRng As Range 


'erases information from arrays if there was any 
Erase CoGrade 

Erase NPSeQuedan 

'------------------------------------------------------------------------- 
'find the last row on the data i want to read 
LastRow = Range("b10000").End(xlUp).Row 
'the relevant data starts on row 34 
ArrayRows = LastRow - 34 + 1 
'redifines the variables with the total numbers of stocks in the portfolio 
ReDim CoGrade(ArrayRows, 1) 
ReDim NPSeQuedan(ArrayRows, 1) 

'reads each relevant number into its proper variable 
CoGrade = Range(Cells(34, 2), Cells(LastRow, 2)) 

'' test 
Set SeQuedanRng = Range(Cells(34, 13), Cells(34 + ArrayRows - 1, 
13)) 
For a = 1 To ArrayRows 
    NPSeQuedan(a, 1) = CoGrade(a, 1) 
Next 
SeQuedanRng.Value = NPSeQuedan 
''' 
end sub 
+2

Bitte geben Sie eine [MCVE]. Sie haben zahlreiche nicht deklarierte und scheinbar nicht initialisierte Variablen (selbst keine gute Programmierpraxis) zusammen mit einer Tabelle, deren Inhalt nicht beschrieben ist. Es ist unmöglich herauszufinden, was hier passiert. –

+0

Danke John, Entschuldigung faul von meinem Teil nicht das ganze Ding kopieren und (Überraschung) Ich bin kein Programmierer selbst, also muss ich alle schlechten Coding Gewohnheiten in der Welt haben. – Frank

+1

Sie müssen nicht alles kopieren - geben Sie nur ein einzelnes * in sich abgeschlossenes * Sub, das das Problem veranschaulicht. Als Vermutung hat das Problem etwas damit zu tun, wie Sie die Variablen deklarieren und initialisieren - was genau Sie nicht zeigen. –

Antwort

1

Hier ist eine andere Lösung (obwohl @SJR 's Idee der Verwendung von 1-dimensionalen Arrays ist gut). Ich habe verschiedene Punkte über Ihren ursprünglichen Code in den Kommentaren zum Code hinzugefügt:

Sub readArray() 
    Dim CoGrade As Variant 'Don't bother with() 
    Dim LastRow As Long 'Integer risks overflow 
    Dim A As Long, ArrayRows As Long 'you use these -- so declare it 
    Dim NPSeQuedan As Variant 'etc. 
    Dim SeQuedanRng As Range 


    'erases information from arrays if there was any 
    'Erase CoGrade -- VBA is garbage collected and these have just been declared, so 100% pointless 

    'Erase NPSeQuedan 

    '------------------------------------------------------------------------- 
    'find the last row on the data i want to read 
    LastRow = Cells(Rows.Count, "B").End(xlUp).Row 'why hard-wire in 10000? 

    'the relevant data starts on row 34 
    ArrayRows = LastRow - 34 + 1 
    'redifines the variables with the total numbers of stocks in the portfolio 
    'ReDim CoGrade(ArrayRows, 1) -- pointless 

    ReDim NPSeQuedan(1 To ArrayRows, 1 To 1) 'this is important for what you are doing 

    'reads each relevant number into its proper variable 

    CoGrade = Range(Cells(34, 2), Cells(LastRow, 2)).Value 

    '' test 
    Set SeQuedanRng = Range(Cells(34, 13), Cells(34 + ArrayRows - 1, 13)) 

    For A = 1 To ArrayRows 
     NPSeQuedan(A, 1) = CoGrade(A, 1) 
    Next 

    SeQuedanRng.Value = NPSeQuedan 'works now! 

End Sub 
1

Sie können es so machen, das einige der von John Coleman gemachten Kommentare enthält.

Sub readArray() 

Dim CoGrade As Variant 
Dim LastRow As Long, ArrayRows as Long, a as Long 
Dim NPSeQuedan() As Variant 
Dim SeQuedanRng As Range 

'find the last row on the data i want to read 
LastRow = Range("b10000").End(xlUp).Row 
'the relevant data starts on row 34 
ArrayRows = LastRow - 34 + 1 
'redifines the variables with the total numbers of stocks in the portfolio 
ReDim NPSeQuedan(1 To ArrayRows) 

'reads each relevant number into its proper variable 
CoGrade = Range(Cells(34, 2), Cells(LastRow, 2)) 
Set SeQuedanRng = Range(Cells(34, 13), Cells(34 + ArrayRows - 1, 13)) 

For a = 1 To ArrayRows 
    NPSeQuedan(a) = CoGrade(a, 1) 
Next 

SeQuedanRng.Value = Application.Transpose(NPSeQuedan) 

End Sub 
Verwandte Themen