我想在VBA中填充Array,使用for-loop但无法执行此操作
Dim MyArray() As Variant
Dim RowCounter As Integer
Dim ColCounter As Integer
Dim rCell As Range
Dim rRng As Range
Set rRng = Sheet1.Range("B10:Z97")
RowCounter = 0
ColCounter = 0
ReDim MyArray(rRng.Rows.Count, rRng.Columns.Count) 'Answer by @varocarbas
For Each rCol In rRng.Columns
For Each rCell In rCol.Rows
If IsNumeric(rCell.Value) And (Not (IsEmpty(rCell.Value))) And (Len(rCell.Value) <> 0) Then
'ReDim Preserve MyArray(RowCounter, ColCounter) -- Old Logic which cause Error
MyArray(RowCounter, ColCounter) = rCell.Value
RowCounter = RowCounter + 1
Else
'Debug.Print rCell.Value & " is not an Integer" & vbNewLine
End If
Next rCell
ColCounter = ColCounter + 1
RowCounter = 0
Next rCol
但ReDim Preserve MyArray(RowCounter, ColCounter)
ReDim Preserve MyArray(1, 0)
下标错误
我想从excel表中读取值,填充数组,然后进行一些计算,并通过Excel的计算值更新Excel中每列的最后一个单元格的值。
代码更新
Function RunSquareOfVariance(temperature As Integer, cellValue As Variant) As Double
RunSquareOfVariance = "=IF((" & temperature + cellValue & ")<0,0,(" & temperature + cellValue & "))*IF((" & temperature + cellValue & ")<0,0,(" & temperature + cellValue & "))"
End Function
如果代码中的 ,我会更改下面的行
MyArray(RowCounter, ColCounter) = RunSquareOfVariance(StantardTemperature, rCell.Value)
现在 MyArray(0,0)值存储为=IF((-16.8)<0,0,(-16.8))*IF((-16.8)<0,0,(-16.8))
但我想存储公式值MyArray(0,0) = ValueOftheFormula
答案 0 :(得分:1)
据我所知,您只能更改最后一个数组维度的大小。
确定我刚检查过,这是真的。根据MSDN:
如果使用Preserve关键字,则只能调整最后一个数组的大小 维度,您根本无法更改维度数。
我不知道你的潜艇的最终目标因此很难提出任何改变。但是,您可以考虑使用数组数组。这种解决方案的语法如下:
Dim arrA() As Variant
Dim arrB() As Variant
...
ReDim Preserve arrA(RowCounter)
ReDim Preserve arrB(ColCounter)
...
arrA(RowCounter) = x
arrB(ColCounter) = y
...
Dim arrAB
arrAB = Array(arrA, arrB)
...
'to get elements of array you need to call it in this way:
arrAB(0)(RowCounter) >> to get x
arrAB(1)(ColCounter) >> to get y
此类解决方案存在一些缺点,但在其他情况下可能有用。
答案 1 :(得分:0)
你可以做到:
Dim rng As Range
Dim myArray() As Variant
Set rRng = Sheet1.Range("B10:Z97")
myArray = rRng.Value
您还需要For Each rCell In rRng.Rows
而不是For Each rCell In rCol.Rows
。否则,就像Kaz说的那样,你只能调整数组的最后一个维度。
答案 2 :(得分:0)
确定问题已解决
MyArray(RowCounter, ColCounter) = Application.Evaluate
(
RunSquareOfVariance(StantardTemperature, rCell.Value)
)
答案 3 :(得分:0)
我可以看到您找到了解决问题的方法。为了将来参考,我想添加一种替代方法来解决这个问题。 特别是,我同意@DavidZemens直接将范围值复制到变量数组的方法。这是一个非常优雅,简单而有效的解决方案。唯一棘手的部分是当您正在查找范围内有空或非数字单元格时,您不希望插入这些值。如果您复制的某些值不是数字,则修改David的方法将起作用。
Sub CopyNumbersToArray()
Dim var As Variant, rng As Range
' Grab the numeric values of the range only. Checking if cell is empty or
' if it has a positive length is not needed
Set rng = Range("B3:K3").SpecialCells(xlCellTypeConstants, xlNumbers)
' Copy the numbers. Note that var=rng.value will not work if rng is not contiguous
rng.Copy
' Paste the numbers temporarily to a range that you do not use
Range("A10000").Resize(1, rng.Count).PasteSpecial xlPasteValues
' Set rng object to point to that range
Set rng = Range(Cells(10000, 1), Cells(10000, rng.Count))
' Store the values that you need in a variant array
var = rng.Value
' Clear the contents of the temporary range
rng.ClearContents
End Sub
对于超过2个维度,锯齿状数组可能是一个很好的方法(正如@KazJaw所建议的那样)