Array (VBA)

Example-1 

Sub ArrayInVBA()

'Dynamic array declaration

Dim arr() As String

Dim i As Integer, j As Integer

Dim r As Range, rng As Range

i = 0

Set rng = Sheet1.Range("a2", Range("a1000").End(xlUp))

'Counting elements in a range

j = rng.Count

j = j - 1

'Resizing Array- Assigning dynamic array a fixed-length.

'We may use Redim Preserve arr(j), if we plan to increase elements in the array which holds some elements already.

ReDim arr(j)

For Each r In rng

    arr(i) = r

    i = i + 1

Next

For i = LBound(arr) To UBound(arr)

    Debug.Print arr(i)

Next

End Sub


Comments

Popular posts from this blog

Power Automate - Automatically fetch data from Power BI in to Excel and Send the copy of the Excel file via Email

Separate Text (Characters) & Numbers from Alpha Numeric String Using Formula and Macro (VBA)

File System Object