删除VBA数组的第一个元素 [英] Remove the first element of a VBA array

查看:365
本文介绍了删除VBA数组的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法删除 VBA 中的数组的第一个元素?

Is there a way to remove the first element of an array in VBA?

像javascript shift()方法?

Something like javascript shift() method?

Option Explicit

Sub Macro1()
Dim matriz() As Variant
Dim x As Variant
matriz = Array(0)

ReDim Preserve matriz(1)
matriz(1) = 5
ReDim Preserve matriz(2)
matriz(2) = 10
ReDim Preserve matriz(3)
matriz(3) = 4

ReDim Preserve matriz(1 To UBound(matriz))

For Each x In matriz
    Debug.Print x
Next x
End Sub

这是重试错误:下标超出范围

推荐答案

VBA中没有直接的方法,但您可以轻松地删除第一个元素:

There is no direct method in VBA but you can remove the first element easily like this:

'Your existing code
'...
'Remove "ReDim Preserve matriz(1 To UBound(matriz))"
For i = 1 To UBound(matriz)
  matriz(i - 1) = matriz(i)
Next i
ReDim Preserve matriz(UBound(matriz) - 1)

这篇关于删除VBA数组的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆