2017-09-26 98 views
0

我正在處理一個列表,並在VBA上進行所有計算,但是當我想將我的列表寫入預定義範圍時,我什麼也得不到。以下是我正在使用的代碼的一個示例。我沒有發佈實際的代碼,因爲它很長,但是這個例子有同樣的問題。將數組寫入一個範圍

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

請給出[mcve]。你有許多未申報的和顯然未初始化的變量(本身不是很好的編程習慣)以及其內容未被描述的電子表格。要弄清楚這裏發生的事情是不可能的。 –

+0

謝謝約翰,對不起,我懶洋洋的抄襲整個事情,而且(驚喜)我不是自己的編碼員,所以我必須擁有世界上所有不好的編碼習慣。 – Frank

+1

您不需要複製所有內容 - 只需提供一個*自包含的* sub來說明問題。作爲猜測,這個問題與你如何聲明和初始化變量有關 - 這正是你沒有顯示的東西。 –

回答

1

這是另一種解決方案(儘管@SJR使用1維數組的想法很好)。我在代碼的註釋中加入了關於您的原代碼的各種要點:

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

你可以這樣做,它包含了John Coleman提出的幾條評論。

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 
相關問題