Excel VBA - 初始化空用户类型并检测空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/721811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA - Initializing Empty User Types and Detecting Nulls
提问by jrsconfitto
I have created a user defined type to contain some data that I will use to populate my form. I am utilizing an array of that user defined type, and I resize that array as I pull data from an off-site server.
我创建了一个用户定义的类型来包含一些我将用来填充我的表单的数据。我正在使用该用户定义类型的数组,并在从异地服务器提取数据时调整该数组的大小。
In order to make my program easier to digest, I have started to split it into subroutines. However, when my program is initialized, I cannot tell when a particular array has been initialized, and so I cannot be certain that I can call a size function to see if the array is empty.
为了让我的程序更容易理解,我开始把它拆分成子程序。但是,当我的程序初始化时,我无法判断某个特定数组何时已初始化,因此我无法确定是否可以调用 size 函数来查看该数组是否为空。
Is there a way to initialize an empty user type or detect a null user type? Currently, I am hard-coding it in and I would prefer a more elegant solution.
有没有办法初始化空用户类型或检测空用户类型?目前,我正在对其进行硬编码,我更喜欢更优雅的解决方案。
回答by Mutant
In addition to isempty(array) solution -
除了 isempty(array) 解决方案 -
If IsNull(array) then
msgbox "array is empty"
End If
回答by shahkalpesh
AFAIK, you cannot check whether user-defined type was initialized before it was sent as an argument to a procedure/function.
AFAIK,您无法检查用户定义的类型在作为参数发送给过程/函数之前是否已初始化。
I am quoting this example from VBA help
我从 VBA 帮助中引用了这个例子
Type StateData
CityCode(1 To 100) As Integer ' Declare a static array.
County As String * 30
End Type
The County field is initialized to some value, which you can use a base value. If the user sets this field explicitly, it means it holds some value & remains uninitialized, otherwise.
County 字段被初始化为某个值,您可以使用基值。如果用户明确设置此字段,则意味着它持有一些值并保持未初始化状态,否则。
for e.g.
例如
Sub main()
Dim example As StateData
MsgBox IsInitialized(example)
Dim example2 As StateData
example2.County = "LA"
MsgBox IsInitialized(example2)
End Sub
Function IsInitialized(arg As StateData) As Boolean
Dim initCounty As String * 30
IsInitialized = (arg.County <> initCounty)
End Function
回答by user53794
Try:
尝试:
dim v
if isempty(v) then
msgbox "is empty"
end if
回答by BradC
If myObjectVariable is Nothing
should work to detect if an object has been initialized.
应该可以检测对象是否已初始化。
Edit: "is nothing" DOES work, if it is an objectvariable:
编辑:“什么都不是”有效,如果它是一个对象变量:
Dim blah As Object
If blah Is Nothing Then
MsgBox "blah is nothing!"
End If
Dim foo as variant
If IsEmpty(foo) Then
MsgBox "foo is empty!"
End If
回答by Mr. Napik
If you need to check whether the whole dynamic array of custom types was initialized or not (not just particular element) in VBA, then this might not be possible directly (as none of the IsEmpty etc. functions works on custom types). However you might be able to easily restructure your program to return an array of custom types of size 0 to indicate that nothing was read/initialized.
如果您需要检查整个自定义类型的动态数组是否在 VBA 中初始化(不仅仅是特定元素),那么这可能无法直接进行(因为 IsEmpty 等函数都不适用于自定义类型)。但是,您可以轻松地重构程序以返回大小为 0 的自定义类型数组,以指示未读取/初始化任何内容。
Private Function doStuff() As customType()
Dim result() As customType
' immediately size it to 0 and assing it as result
ReDim result(0)
doStuff = vysledek
' do real stuff, ... premature "Exit Function" will return an array of size 0
' possibly return initialized values
End Function
' then you can all
If (UBound(tabulky) = 0) Then
MsgBox "Nope, it is not initialized."
End If