我正在尝试获取范围的值并将它们放入数组中。我有以下代码,效果很好:
Dim rg As Range
Dim Inputs() As Variant
Set rg = ("A1:B10")
Inputs = rg
但是当我尝试使用Long而不是Variant时,我得到了一个Type Mismatch错误,即使所有的值都是数字。是否可以创建Long数组而不是Variant数组?
答案 0 :(得分:0)
或许这样的事情:
Option Explicit
Option Base 1
Sub RangeToLong()
Dim rng As Range
Dim mylong() As Long, i As Long
Set rng = Sheet3.Range("A1:A10")
ReDim mylong(rng.Cells.Count)
'Debug.Print LBound(mylong), UBound(mylong)
For i = LBound(mylong) To UBound(mylong)
mylong(i) = rng(i)
'Debug.Print mylong(i)
Next
End Sub
编辑1:对于2D长阵列
Option Explicit
Option Base 1
Sub RangeToLong()
Dim rng As Range
Dim mylong() As Long, i As Long, j As Long
Set rng = Sheet3.Range("A1:B10")
ReDim mylong(1 To rng.Rows.Count, 1 To rng.Columns.Count)
'Debug.Print LBound(mylong), UBound(mylong)
For i = LBound(mylong, 1) To UBound(mylong, 1)
For j = LBound(mylong, 2) To UBound(mylong, 2)
mylong(i, j) = rng(i, j)
'Debug.Print mylong(i, j)
Next
Next
End Sub