2014-09-08 14:39:54 +0000 2014-09-08 14:39:54 +0000
14
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

回答 (7)

10
10
10
2014-09-08 14:50:15 +0000

これを試してみてください[EDITED]:

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
8
8
8
2014-09-09 12:00:57 +0000

コレクションを使って、後から配列にコピーすることで解決しました。

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
3
3
3
2015-01-15 23:33:57 +0000

また、実際にバリアントの配列が必要な場合(例えば Shapes.Range のようなプロパティに渡す場合)は、次のようにします。

1
1
1
2014-09-09 20:08:52 +0000

範囲が単一のベクトルで、列内で行数が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
0
0
2019-08-09 05:00:33 +0000
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
0
0
0
2014-10-04 17:03:12 +0000

Thx。2つの関数で同じことをすると、私のような他のnoobsを助けることができる場合 :

コレクション

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

1次元配列 :

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
0
0
2018-04-08 02:00:48 +0000

答えは、(ReDim問題なしで)で合格した回答の中にあります: https://stackoverflow.com/questions/12663879/adding-values-to-variable-array-vba

レジュメの中にあります:

Dim aArray() As Single ' or whatever data type you wish to use
ReDim aArray(1 To 1) As Single
If strFirstName = "henry" Then
    aArray(UBound(aArray)) = 123.45
    ReDim Preserve aArray(1 To UBound(aArray) + 1) As Single
End If