我写了一个CSV导入功能。 CSV文件分为列和行,然后保存到多维数组中。
有一点,我需要将多维数组设置为Empty
,因为以下If-condition检查IsEmpty()
。
我已经尝试过了:
aMultidimArray = Empty
aMultidimArray = Nothing
ReDim aMultidimArray(0,0)
Erase aMultidimArray
a = Split(sData, sDelimiter)
For Each x In a
'*** Resize and write into multidimensional array
ReDim Preserve aMultidimArray(iFirstDim, iSecondDim)
aMultidimArray(i - 1,iSecondDim) = x
If (i = iFirstDim + 1) Then
i = 0
End If
i = i + 1
Next
'***Empty array here
???
'***
GetDataFromCSV = aMultidimArray
'**** other script
If IsEmpty(GetDataFromCSV) Then
Do stuff
End If
IsEmpty(GetDataFromCSV)
应该返回true,但我无法处理它。它不是关于清除阵列。这是为了让它再次初始化。如果这是可能的话。
作为一种解决方法(或者可能只是一个更好的解决方案),我刚刚打电话给Exit function
。因此,返回值为Empty
and,IsEmpty
condition将返回True
。
如有疑问,请阅读documentation:
如果变量未初始化,或者明确设置为
IsEmpty
,则True
返回Empty
;否则,它返回False
。如果expression包含多个变量,则始终返回False
。
由于你的变量绝对是初始化的,即使你从数组中删除所有值,为什么你会期望IsEmpty
返回除False
之外的任何东西?
根据您实际想要使用代码实现的内容,您可以检查数组的尺寸:
If UBound(GetDataFromCSV, 1) = -1 And UBound(GetDataFromCSV, 2) = -1 Then
'array is a 0x0 array
End If
如果您的数组不是零大小,您可能需要迭代所有字段并检查它们是否为空。
如果您认为需要将定义为数组的变量重置为Empty
,我怀疑您在某处出现了设计错误,因此我强烈建议您返回并修复设计。但是,如果您出于某种原因绝对必须将数组变量重置为Empty
,您应该能够这样做:
ReDim a(2, 5) 'define variable as array
...
Dim a 're-define variable as regular variable; does not clear the value
a = Empty 'clear variable