将元素添加到数组的末尾


14

我想在VBA数组的末尾添加一个值。我怎样才能做到这一点?我无法在线找到一个简单的示例。这是一些伪代码,显示我想做的事。

Public Function toArray(range As range)
 Dim arr() As Variant
 For Each a In range.Cells
  'how to add dynamically the value to end and increase the array?
   arr(arr.count) = a.Value 'pseudo code
 Next
toArray= Join(arr, ",")
End Function

是将值添加到现有数组末尾的想法吗?还是像您只想将范围加载到数组中的示例所示?如果是后者,为什么不使用单缸arr = Range.Value
Excellll 2014年

Answers:


10

试试这个[编辑]:

Dim arr() As Variant ' let brackets empty, not Dim arr(1) As Variant !

For Each a In range.Cells
    ' change / adjust the size of array 
    ReDim Preserve arr(1 To UBound(arr) + 1) As Variant

    ' add value on the end of the array
    arr (UBound(arr)) = a.value
Next

谢谢,但是不幸的是,这不起作用,UBound(arr)需要arr用一些尺寸进行初始化,例如,Dim arr(1) As Variant但是后来ReDim Preserve失败了,并说数组已经确定了尺寸?换句话说,您不能在VBA中重新排列数组吗?
megloff 2014年


好吧,来自msdn的示例在excel vba中也不起作用。同样的错误,抱怨数组已经确定了尺寸
megloff 2014年

看来我应该使用而不是数组a,Collection然后再将其转换为数组。还有其他建议吗?
megloff 2014年

2
谢谢,但是它仍然不能用这种方法,因为如前所述,它UBound(arr)需要一个已经确定尺寸的数组。好吧,看来我必须改用集合。无论如何,谢谢您
megloff 2014年

9

我通过使用Collection解决了该问题,然后将其复制到数组中。

Dim col As New Collection
For Each a In range.Cells
   col.Add a.Value  '  dynamically add value to the end
Next
Dim arr() As Variant
arr = toArray(col) 'convert collection to an array

Function toArray(col As Collection)
  Dim arr() As Variant
  ReDim arr(0 To col.Count-1) As Variant
  For i = 1 To col.Count
      arr(i-1) = col(i)
  Next
  toArray = arr
End Function

2
如果要使用集合,则最好使用字典对象。`Set col = CreateObject(“ Scripting.Dictionary”)`然后您可以将键直接输出为数组,并跳过添加的函数:`arr = col.keys`<= Array
B Hart

3

这是我使用变体(数组)变量的方法:

Dim a As Range
Dim arr As Variant  'Just a Variant variable (i.e. don't pre-define it as an array)

For Each a In Range.Cells
    If IsEmpty(arr) Then
        arr = Array(a.value) 'Make the Variant an array with a single element
    Else
        ReDim Preserve arr(UBound(arr) + 1) 'Add next array element
        arr(UBound(arr)) = a.value          'Assign the array element
    End If
Next

或者,如果您实际上确实需要一个Variants数组(例如,传递给Shapes.Range之类的属性),则可以采用以下方式:

Dim a As Range
Dim arr() As Variant

ReDim arr(0 To 0)                       'Allocate first element
For Each a In Range.Cells
    arr(UBound(arr)) = a.value          'Assign the array element
    ReDim Preserve arr(UBound(arr) + 1) 'Allocate next element
Next
ReDim Preserve arr(LBound(arr) To UBound(arr) - 1)  'Deallocate the last, unused element

谢谢,使用ReDim arr(0到0),然后分配下一个对我
有用的

1

如果范围是单个向量,并且在列中的行数少于16,384,则可以使用以下代码:

Option Explicit
Public Function toArray(RNG As Range)
    Dim arr As Variant
    arr = RNG

    With WorksheetFunction
        If UBound(arr, 2) > 1 Then
            toArray = Join((.Index(arr, 1, 0)), ",")
        Else
            toArray = Join(.Transpose(.Index(arr, 0, 1)), ",")
        End If
    End With
End Function

0

谢谢。如果可以帮助像我这样的其他菜鸟,请使用2个功能执行相同的操作:

采集

Function toCollection(ByVal NamedRange As String) As Collection
  Dim i As Integer
  Dim col As New Collection
  Dim Myrange As Variant, aData As Variant
  Myrange = Range(NamedRange)
  For Each aData In Myrange
    col.Add aData '.Value
  Next
  Set toCollection = col
  Set col = Nothing
End Function

一维阵列

Function toArray1D(MyCollection As Collection)
    ' See http://superuser.com/a/809212/69050


  If MyCollection Is Nothing Then
    Debug.Print Chr(10) & Time & ": Collection Is Empty"
    Exit Function
  End If

  Dim myarr() As Variant
  Dim i As Integer
  ReDim myarr(1 To MyCollection.Count) As Variant

  For i = 1 To MyCollection.Count
      myarr(i) = MyCollection(i)
  Next i

  toArray1D = myarr
End Function

用法

Dim col As New Collection
Set col = toCollection(RangeName(0))
Dim arr() As Variant
arr = toArray1D(col)
Set col = Nothing


0
Dim arr()  As Variant: ReDim Preserve arr(0) ' Create dynamic array

' Append to dynamic array function
Function AppendArray(arr() As Variant, var As Variant) As Variant
    ReDim Preserve arr(LBound(arr) To UBound(arr) + 1) ' Resize array, add index
    arr(UBound(arr) - 1) = var ' Append to array
End Function
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.