Excelシート上の選択したデータを配列に転送して新しいスプレッドシートにデータを出力するコードを記述しています。しかし、selectArr(i - 1)をスクロールすると値が表示されても、 "Subscript Out of Range"エラーが表示されます。ここに私のコードは次のとおりです。値が定義されていても添え字が範囲外である
Sub Marascuilo()
Dim numRows As Integer 'Number of rows selected
numRows = Selection.Rows.Count
Dim selectArr() As Double 'Array containing numbers from selected cells
selectArr = loadArr(numRows) 'Load values into array
For i = 2 To UBound(selectArr) - LBound(selectArr) + 2
Sheets("Sheet 4").Cells(i, 2).Value = selectArr(i - 1)
Next
End Sub
'This function loads the values from the selected cells into selectArr.
Function loadArr(numRows) As Double()
Dim ResultArray() As Double
r = 1
For Each v In Selection
ReDim Preserve ResultArray(1 To r)
If v <> "" Then
ResultArray(r) = v.Value
r = r + 1
End If
Next
loadArr = ResultArray
End Function
すべてのアイデアは、私はこの問題を解決する方法として?
ありがとうございます!
ジェイ
問題は、シート(「シート4」)ではなく、selectArr(i-1)で確認できますか?セル(i、2)? –
オペレーションの順序がわからない場合は、おそらくこれが必要です... 'i = 2 To(UBound(selectArr) - LBound(selectArr)+ 2)' – xQbert
私はToddと同意します。 'Sheets(" Sheet 4 ")は' Sheets( "Sheet4") 'でなければならないと感じています:) –