Excel VBA: удаление первого элемента из массива

Чтобы удалить первый элемент из массива в Excel VBA, вы можете использовать несколько методов. Вот несколько примеров:

Метод 1: сдвиг элементов

Sub RemoveFirstElement()
    Dim arr() As Variant
    Dim i As Long

    ' Initialize the array
    arr = Array("A", "B", "C", "D", "E")

    ' Shift elements to the left
    For i = LBound(arr) + 1 To UBound(arr)
        arr(i - 1) = arr(i)
    Next i

    ' Resize the array to remove the last element
    ReDim Preserve arr(LBound(arr) To UBound(arr) - 1)

    ' Display the modified array
    For i = LBound(arr) To UBound(arr)
        Debug.Print arr(i)
    Next i
End Sub

Метод 2. Использование цикла

Sub RemoveFirstElement()
    Dim arr() As Variant
    Dim i As Long

    ' Initialize the array
    arr = Array("A", "B", "C", "D", "E")

    ' Remove the first element
    For i = LBound(arr) + 1 To UBound(arr)
        arr(i - 1) = arr(i)
    Next i

    ' Resize the array to remove the last element
    ReDim Preserve arr(LBound(arr) To UBound(arr) - 1)

    ' Display the modified array
    For i = LBound(arr) To UBound(arr)
        Debug.Print arr(i)
    Next i
End Sub

Способ 3: использование функции Копировать

Sub RemoveFirstElement()
    Dim arr() As Variant
    Dim i As Long

    ' Initialize the array
    arr = Array("A", "B", "C", "D", "E")

    ' Remove the first element using the Copy function
    arr = WorksheetFunction.Copy(arr, 1)

    ' Display the modified array
    For i = LBound(arr) To UBound(arr)
        Debug.Print arr(i)
    Next i
End Sub