2017-09-26 62 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