检查数组是否为空(VBA Excel) [英] check if array is empty (vba excel)

查看:925
本文介绍了检查数组是否为空(VBA Excel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

These if ... then statements are getting the wrong results in my opinion. The first is returning the value 'false' when it should be 'true'. The fourth returns the right value. The second and third return an error.

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

推荐答案

在您的代码的第一条语句中,Arr1成为'Variant'的数组:

Arr1 becomes an array of 'Variant' by the first statement of your code:

Dim arr1() As Variant

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

Array of size zero is not empty, as like an empty box exists in real world.

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

If you define a variable of 'Variant', that will be empty when it is created.

以下代码将显示空".

Dim a as Variant

If IsEmpty(a) then
  MsgBox("Empty")
Else
  MsgBox("Not Empty")
End If

这篇关于检查数组是否为空(VBA Excel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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