用于将数组粘贴到范围上的索引符号
Index notation to paste an array on a range
以下代码在一个模块中
Option Explicit
Private sub paste_arr()
Dim arr(2)
arr(0) = 2
arr(1) = 3
arr(2) = 5
with sh_array
.Range(.Cells(2,5), .Cells(4,5)) = arr
end with
结果如下数组
2
2
2
好像只粘贴了列表中的第一项,但我想粘贴整个数组。
其他解决方案似乎没有使用索引表示法。例如以下
or
编辑:
根据给出的答案,我不认为每个人都理解我希望使用索引符号 Cells(1,1) 而不是 A1 符号 "A1".这里是 link 我用索引符号/引用索引号表示的意思。 Index numbers VBA Docs.
使用索引表示法的愿望导致了另一种(也许不是那么简单)的表示法。因为 .Resize() 函数的索引符号如下:
With sh_array
.Range(*code*).Resize(*code*)
' or
.Range(.Cells(1,1).Resize(*code*)
end With
结果
Method 'Range' of object '_Worksheet' Failed
感谢@FaneDuru
我错误地假设 Excel 会发现垂直传递一维数组会为我调整数组。
所以答案是
Option Explicit
Private sub paste_arr()
Dim arr(2)
arr(0) = 2
arr(1) = 3
arr(2) = 5
with sh_array
.Range(.Cells(2,5), .Cells(4,5)) = Application.Transpose(arr)
' or better if length of array is unknown
.Range(.Cells(2,5), .Cells(UBound(arr) + 2,5)) = Application.Transpose(arr)
' next option based on .resize
.Cells(2,5).Resize(UBound(arr) + 1, 1) = Application.Transpose(arr)
end with
结果如下数组
2
3
5
一维数组到一个范围
Option Explicit
Private Sub ZeroBased()
' ' 1. Either
' 'Dim arr(2) As Variant
' ' More correct is (Option Base related):
' Dim arr(0 To 2) As Variant ' ensure zero-based
' arr(0) = 2
' arr(1) = 3
' arr(2) = 5
' 2. Or
'Dim arr As Variant: arr = Array(2, 3, 5)
' More correct is (Option Base related):
Dim arr As Variant: arr = VBA.Array(2, 3, 5) ' ensure zero-based
Dim cCount As Long
'cCount = UBound(arr) - LBound(arr) + 1 ' 2 - 0 + 1 = 3
' Since in this case LBound(arr) is 0, you can simplify:
cCount = UBound(arr) + 1 ' 2 + 1 = 3
' A simple way to write the values to the Immediate window (Ctrl+G).
Debug.Print Join(arr, ",") ' delimited
Debug.Print Join(arr, vbLf) ' one below the other
' Write to a range
With Sheet1
' Write to a one-row range:
.Range("A1").Resize(, cCount).Value = arr ' A1:C1 ' note the comma
' Write to a one-column range:
.Range("A3").Resize(cCount).Value = Application.Transpose(arr) ' A3:A5
End With
' .Resize(, cCount) is short for .Resize(1, cCount)
' .Resize(cCount) is short for .Resize(cCount, 1)
' Interestingly, you could easily write only the first 2 items
' (think first 100 out of 1000 items):
With Sheet1
' Write to a one-row range:
.Range("E1").Resize(, 2).Value = arr ' E1:F1 ' note the comma
' Write to a one-column range:
.Range("A7").Resize(2).Value = Application.Transpose(arr) ' A7:A8
End With
End Sub
以下代码在一个模块中
Option Explicit
Private sub paste_arr()
Dim arr(2)
arr(0) = 2
arr(1) = 3
arr(2) = 5
with sh_array
.Range(.Cells(2,5), .Cells(4,5)) = arr
end with
结果如下数组
2
2
2
好像只粘贴了列表中的第一项,但我想粘贴整个数组。
其他解决方案似乎没有使用索引表示法。例如以下
编辑:
根据给出的答案,我不认为每个人都理解我希望使用索引符号 Cells(1,1) 而不是 A1 符号 "A1".这里是 link 我用索引符号/引用索引号表示的意思。 Index numbers VBA Docs.
使用索引表示法的愿望导致了另一种(也许不是那么简单)的表示法。因为 .Resize() 函数的索引符号如下:
With sh_array
.Range(*code*).Resize(*code*)
' or
.Range(.Cells(1,1).Resize(*code*)
end With
结果
Method 'Range' of object '_Worksheet' Failed
感谢@FaneDuru
我错误地假设 Excel 会发现垂直传递一维数组会为我调整数组。
所以答案是
Option Explicit
Private sub paste_arr()
Dim arr(2)
arr(0) = 2
arr(1) = 3
arr(2) = 5
with sh_array
.Range(.Cells(2,5), .Cells(4,5)) = Application.Transpose(arr)
' or better if length of array is unknown
.Range(.Cells(2,5), .Cells(UBound(arr) + 2,5)) = Application.Transpose(arr)
' next option based on .resize
.Cells(2,5).Resize(UBound(arr) + 1, 1) = Application.Transpose(arr)
end with
结果如下数组
2
3
5
一维数组到一个范围
Option Explicit
Private Sub ZeroBased()
' ' 1. Either
' 'Dim arr(2) As Variant
' ' More correct is (Option Base related):
' Dim arr(0 To 2) As Variant ' ensure zero-based
' arr(0) = 2
' arr(1) = 3
' arr(2) = 5
' 2. Or
'Dim arr As Variant: arr = Array(2, 3, 5)
' More correct is (Option Base related):
Dim arr As Variant: arr = VBA.Array(2, 3, 5) ' ensure zero-based
Dim cCount As Long
'cCount = UBound(arr) - LBound(arr) + 1 ' 2 - 0 + 1 = 3
' Since in this case LBound(arr) is 0, you can simplify:
cCount = UBound(arr) + 1 ' 2 + 1 = 3
' A simple way to write the values to the Immediate window (Ctrl+G).
Debug.Print Join(arr, ",") ' delimited
Debug.Print Join(arr, vbLf) ' one below the other
' Write to a range
With Sheet1
' Write to a one-row range:
.Range("A1").Resize(, cCount).Value = arr ' A1:C1 ' note the comma
' Write to a one-column range:
.Range("A3").Resize(cCount).Value = Application.Transpose(arr) ' A3:A5
End With
' .Resize(, cCount) is short for .Resize(1, cCount)
' .Resize(cCount) is short for .Resize(cCount, 1)
' Interestingly, you could easily write only the first 2 items
' (think first 100 out of 1000 items):
With Sheet1
' Write to a one-row range:
.Range("E1").Resize(, 2).Value = arr ' E1:F1 ' note the comma
' Write to a one-column range:
.Range("A7").Resize(2).Value = Application.Transpose(arr) ' A7:A8
End With
End Sub