检查数组是否为空(vba excel)

use*_*178 5 excel vba excel-vba

这些if ... then语句在我看来得到了错误的结果.第一个是当值为'true'时返回值'false'.第四个返回正确的值.第二个和第三个返回错误.

Sub empty_array()
  Dim arr1() As Variant

  If IsEmpty(arr1) Then
    MsgBox "hey"
  End If

  If IsError(UBound(arr1)) Then
    MsgBox "hey"
  End If

  If IsError(Application.match("*", (arr1), 0)) Then
    MsgBox "hey"
  End If

  ReDim arr1(1)
  arr1(1) = "hey"

  If IsEmpty(arr1) Then
    MsgBox "hey"
  End If
End Sub
Run Code Online (Sandbox Code Playgroud)

小智 12

我可能有点晚了,但是以下简单的内容适用于字符串数组:

Dim files() As String

files = "..." 'assign array

If Not Not files Then
    For i = LBound(files) To UBound(files)
        'do stuff, array is not empty
    Next
End If

Run Code Online (Sandbox Code Playgroud)

这就是所有的代码。

  • 这应该是未分配数组的最佳答案,另请参阅 [RIP_CP](http://www.cpearson.com/excel/vbaarrays.htm) 进行深入研究 (2认同)

小智 11

添加到此:这取决于您的数组定义为什么。考虑:

dim a() as integer
dim b() as string
dim c() as variant

'these doesn't work
if isempty(a) then msgbox "integer arrays can be empty"
if isempty(b) then msgbox "string arrays can be empty"

'this is because isempty can only be tested on classes which have an .empty property

'this do work
if isempty(c) then msgbox "variants can be empty"
Run Code Online (Sandbox Code Playgroud)

所以,我们能做些什么?在 VBA 中,我们可以查看是否可以触发错误并以某种方式处理它,例如

dim a() as integer
dim bEmpty as boolean

bempty=false

on error resume next
bempty=not isnumeric(ubound(a))
on error goto 0
Run Code Online (Sandbox Code Playgroud)

但这真的很笨拙......更好的解决方案是声明一个布尔变量(公共或模块级别最好)。当数组第一次初始化时,然后设置这个变量。因为它是一个同时声明的变量,如果它失去了它的值,那么你知道你需要重新初始化你的数组。但是,如果它已初始化,那么您所做的就是检查布尔值的值,这是低成本的。这取决于低成本是否重要,以及您是否需要经常检查它。

option explicit

'declared at module level
dim a() as integer
dim aInitialised as boolean

sub DoSomethingWithA()
if not aInitialised then InitialiseA
'you can now proceed confident that a() is intialised
end sub

sub InitialiseA()
'insert code to do whatever is required to initialise A
'e.g. 
redim a(10)
a(1)=123
'...
aInitialised=true
end sub
Run Code Online (Sandbox Code Playgroud)

您可以做的最后一件事是创建一个函数;在这种情况下,这将需要依赖于笨拙的错误方法。

function isInitialised(byref a() as variant) as boolean
isInitialised=false
on error resume next
isinitialised=isnumeric(ubound(a))
end function
Run Code Online (Sandbox Code Playgroud)


小智 10

Arr1成为代码的第一个语句的'Variant'数组:

Dim arr1() As Variant
Run Code Online (Sandbox Code Playgroud)

大小为零的数组不为空,就像现实世界中存在空盒子一样.

如果定义变量'Variant',则在创建时将为空.

以下代码将显示"Empty".

Dim a as Variant

If IsEmpty(a) then
  MsgBox("Empty")
Else
  MsgBox("Not Empty")
End If
Run Code Online (Sandbox Code Playgroud)


小智 5

我会这样做

if isnumeric(ubound(a)) = False then msgbox "a is empty!"
Run Code Online (Sandbox Code Playgroud)


Kev*_*ner 5

@jeminar 有上面的最佳解决方案。

不过我清理了一下。

我建议将其添加到 FunctionsArray 模块中

  • isInitialised=false 不需要,因为创建时布尔值是假的
  • On Error GoTo 0在错误块内包装和缩进类似于with可见性块的代码。应尽可能避免使用这些方法,但是... VBA ...
Function isInitialised(ByRef a() As Variant) As Boolean
    On Error Resume Next
    isInitialised = IsNumeric(UBound(a))
    On Error GoTo 0
End Function

Run Code Online (Sandbox Code Playgroud)