for each-loop を使用して VBA に Array を設定したいのですが、それができません
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))
しかし、式Withingの値を保存したいMyArray(0,0) = ValueOftheFormula