通过变量的完整类型,我指的是您在即时窗口中获得的信息类型:
我想使用VBA动态确定类型信息。函数TypeName()
没有做我想要的,因为它返回变量的子类型,并且不区分例如包含范围的变量变量,包含范围的对象变量,以及包含范围的范围变量。
作为初步步骤,我编写了一个函数,用于检测变量是否传递给它。它的工作原理是利用传递引用语义。代码使用其参数执行的操作只能通过变量完成,因此如果传递的变量实际上不是变量,则会触发错误:
Function IsVariant(var As Variant) As Boolean
Dim temp As Variant
Dim isVar As Boolean
If IsObject(var) Then
Set temp = var
Else
temp = var
End If
On Error Resume Next
Set var = New Collection
var = "test"
If Err.Number > 0 Then
isVar = False
Else
isVar = True
End If
On Error GoTo 0
If IsObject(temp) Then
Set var = temp
Else
var = temp
End If
IsVariant = isVar
End Function
基于此,我写道:
Function FullType(var As Variant) As String
If IsVariant(var) Then
FullType = "Variant/" & TypeName(var)
Else
FullType = TypeName(var)
End If
End Function
测试代码:
Sub TestTypes()
Dim R As Range
Dim Ob As Object
Dim i As Integer
Dim v1 As Variant
Dim v2 As Variant
v1 = 10
i = 10
Set v2 = Range("A1")
Set Ob = Range("A2")
Set R = Range("A3")
Debug.Print "v1: " & FullType(v1)
Debug.Print "i: " & FullType(i)
Debug.Print "v2: " & FullType(v2)
Debug.Print "Ob: " & FullType(Ob)
Debug.Print "R: " & FullType(R)
End Sub
输出:
v1: Variant/Integer
i: Integer
v2: Variant/Range
Ob: Range
R: Range
这几乎是我想要的 - 但不区分保持范围的对象变量和保持范围的范围变量。我尝试编写一个名为IsTypeObject
的函数,该函数与IsVariant
类似,但似乎无法使其工作:
Function IsTypeObject(var As Variant) As Boolean
Dim temp As Variant
Dim isGeneric As Boolean
If (Not IsObject(var)) Or IsVariant(var) Then
IsTypeObject = False
Exit Function
End If
Set temp = var
On Error Resume Next
Set var = New Collection
Set var = ActiveWorkbook
If Err.Number > 0 Then
isGeneric = False
Else
isGeneric = True
End If
On Error GoTo 0
Set var = temp
IsTypeObject = isGeneric
End Function
测试:
Sub test()
Dim R As Range
Set R = Range("A1")
Debug.Print IsTypeObject(R)
End Sub
但这会打印True
,即使我认为使IsVariant
工作的相同的传递引用语义也应该使IsTypeObject
有效(你不能指定)一系列的集合)。我尝试了各种调整,但似乎无法区分通用对象变量和特定对象变量,例如范围变量。
那么 - 关于如何动态获取变量的完整类型的任何想法? (动机是调试日志实用程序的一部分)
答案 0 :(得分:17)
是的,你可以做到这一点:它需要一点指针知识和解除引用的概念......
这是执行此操作的代码:
Public Function VariantTypeName(ByRef MyVariant) As String
' Returns the expanded type name of a variable, indicating
' whether it's a simple data type (eg: Long Integer), or a
' Variant containing data of that type, eg: "Variant/Long"
Dim iType As Integer
Const VT_BYREF = &H4000&
CopyMemory iType, MyVariant, 2
' iType now contains the VarType of the incoming parameter
' combined with a bitwise VT_BYREF flag indicating that it
' was passed by reference. In other words, it's a pointer,
' not the data structure of the variable (or a copy of it)
' So we should have VT_BYREF - and we'd always expect to
' when MyVariant is a Variant, as variants are a structure
' which uses a pointer (or pointers) to the stored data...
' However, the VBA implementation of a variant will always
' dereference the pointer - all the pointers - passing us
' straight to the data, stripping out all that information
' about references...
If (iType And VT_BYREF) = VT_BYREF Then
' Bitwise arithmetic detects the VT_BYREF flag:
VariantTypeName = TypeName(MyVariant)
Else
' No VT_BYREF flag. This is a Variant, not a variable:
VariantTypeName = "Variant/" & TypeName(MyVariant)
End If
End Function
(CopyMemory
API函数的声明是下面的几段。
这需要一些解释,因为Visual Basic语言系列旨在保护您免受变量及其类型的实现细节 - 特别是指针的概念 - 并且我的代码确实涉及一些横向思维。
简单来说,你的变量有一个名字 - 像' intX'你在代码中看到了;分配用于包含实际数据的内存区域;和该记忆的地址。
该地址实际上是用于分配给变量的内存的开始,并且变量将实现作为由实际数据的偏移定义的内存中的结构,具有大小(或数据的长度 - 对于复杂类型,通过偏移到地址到内存中的其他结构。这些大小和偏移是预定义的:它们是变量的实际实现,我们VBA开发人员很少需要了解它 - 我们声明了类型,并且它都是为我们完成的。
今天你需要知道的第一件事是VBA中变量地址的前两个字节是the enumerated var type:这就是VarType()函数的工作原理。
当程序通过该地址时,它不会将复制的数据分配传递到内存中,而是将该地址作为指针传递。是的,我过于简单化了一些,但VBA开发人员确实知道获取指针和数据副本之间的区别:它位于ByRef
和ByVal
标识符中我们在声明函数时使用传入参数。
VBA和VB非常非常善于保护我们免受细节影响:很好,我们无法使用VarType
和TypeName
来检测我们已经通过价值或对它的引用;甚至是对参考文献的引用。
这很重要,因为变量是其他变量的包装器,并且该结构为您提供了一个指向它包含的变量的指针,用var类型来描述它:但是,我们无法知道VBA - 我们直接通过地址指示的行,一直到我们将要使用的数据,而VBA varType
从未告诉我们,我们通过几次跳跃间接地到达那里由指针定义的连续地址。
但是,如果您准备使用API调用查看指针后面的两个字节,那么该信息确实存在。
正如我所说,这两个字节包含var类型 - 但还有更多:它们包含var类型和一个按位标记VT_BYREF
,表明这是一个引用或指针。存储的数据类型,而不是数据本身。所以这段代码会可靠地告诉你你的var类型,有一点横向思维来克服VBA是有帮助的,当我们相反它不是:
Public Function DereferencedType(ByRef MyVar) As Long
Dim iType As Integer
Const VT_BYREF = &H4000&
' The first two bytes of a variable are the type ID with a
' bitwise OR to VT_BYREF if we were passed the variable by
' reference... Which is exactly what this function does:
CopyMemory iType, MyVar, 2
DereferencedType = iType ' Mod VT_BYREF
'Use "Mod VT_BYREF" to separate out the type if you want
End Function
乍一看,这个功能似乎是弄巧成拙的:我通过引用传递变量 - 变量或简单类型,因此它总是与VT_BYREF
结合使用。而且我已经评论出了' modulo'无论如何算术...
......以及它的实际工作方式:传递一个简单的变量,并告诉你通过引用传递了变量:
Dim str1 As String
str1 = "One Hundred"
Debug.Print "String Variable: " & DereferencedType(str1)
...并获得输出vbString OR VT_BYREF
:
String Variable: 16392
但是如果你传递我们的函数一个字符串变体,VBA的Variant实现将保护你免受关于指针和通过引用传递的所有复杂性 - 一直到数据 - 并为您的数据提供所有不需要的信息:
Dim varX As Variant
varX = "One Hundred"
Debug.Print "String Variant: " & DereferencedType(varX)
......你得到了输出:
String Variant: 8
我将让您使用VT_BYREF
对返回的值进行OR或NOT操作编码,以便为您提供变体/'变量/字符串和变体/长输出的扩展字符串描述符的标签。
[编辑:这样做,它位于答案的顶部,实现为VariantTypeName
]
我建议您如图所示声明CopyMemory API调用,并为您可能遇到的所有环境使用条件编译器常量:
#If VBA7 And Win64 Then ' 64 bit Excel under 64-bit Windows
' Use LongLong and LongPtr
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As LongLong)
#ElseIf VBA7 Then ' 64 bit Excel in all environments
' Use LongPtr only, LongLong is not available
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
#Else ' 32 bit Excel
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
#End If
与此同时,更难的问题 - 获得变体/对象/范围 - 将需要进一步的工作。我可以告诉你,你的变体包含一个范围,我可以告诉它它是一个变体而不是它本身就是一个范围:但是我不能在声明链中说明一个对象被声明为& #39;对象'现在它指向一个范围:
VarX is set equal to a range object variable: varX: type=8204 Range Dereferenced Type=9 rng1: type=8204 Range Dereferenced Type=16393
VarX is set equal to a range object's value, a 2-dimensional array: varX: type=8204 Variant() Dereferenced Type=8204 arr1: type=8204 Variant() Dereferenced Type=8204
The array variable is erased to Empty(). Inspect varX: varX: type=8204 Variant() Dereferenced Type=8204 arr1: type=8204 Variant() Dereferenced Type=8204
VarX is set equal to an 'object' variable, which has been set to a range: varX: type=8204 Range Dereferenced Type=9 obj1: type=8204 Range Dereferenced Type=16393
这是生成该代码的代码,以及完整输出:
Public Sub TestVar()
Dim varX As Variant
Dim str1 As String
Dim lng1 As Long
Dim rng1 As Excel.Range
Dim arr1 As Variant
Dim obj1 As Object
Debug.Print "Uninitialised:"
Debug.Print
Debug.Print vbTab & "varX: type=" & VarType(varX) & vbTab & vbTab & TypeName(varX) & vbTab & "Dereferenced Type=" & DereferencedType(varX)
Debug.Print vbTab & "str1: type=" & VarType(str1) & vbTab & vbTab & TypeName(str1) & vbTab & "Dereferenced Type=" & DereferencedType(str1)
Debug.Print vbTab & "lng1: type=" & VarType(lng1) & vbTab & vbTab & TypeName(lng1) & vbTab & "Dereferenced Type=" & DereferencedType(lng1)
Debug.Print
varX = "One Hundred"
str1 = "One Hundred"
lng1 = 100
Debug.Print "varX and str1 are populated with the same literal:"
Debug.Print
Debug.Print vbTab & "varX: type=" & VarType(varX) & vbTab & vbTab & TypeName(varX) & vbTab & "Dereferenced Type=" & DereferencedType(varX)
Debug.Print vbTab & "str1: type=" & VarType(str1) & vbTab & vbTab & TypeName(str1) & vbTab & "Dereferenced Type=" & DereferencedType(str1)
Debug.Print vbTab & "lng1: type=" & VarType(lng1) & vbTab & vbTab & TypeName(lng1) & vbTab & "Dereferenced Type=" & DereferencedType(lng1)
Debug.Print
varX = 100
lng1 = 100
Debug.Print "varX and lng1 are populated with the same integer:"
Debug.Print
Debug.Print vbTab & "varX: type=" & VarType(varX) & vbTab & vbTab & TypeName(varX) & vbTab & "Dereferenced Type=" & DereferencedType(varX)
Debug.Print vbTab & "str1: type=" & VarType(str1) & vbTab & vbTab & TypeName(str1) & vbTab & "Dereferenced Type=" & DereferencedType(str1)
Debug.Print vbTab & "lng1: type=" & VarType(lng1) & vbTab & vbTab & TypeName(lng1) & vbTab & "Dereferenced Type=" & DereferencedType(lng1)
Debug.Print
varX = str1
Debug.Print "VarX is set equal to str1:"
Debug.Print
Debug.Print vbTab & "varX: type=" & VarType(varX) & vbTab & vbTab & TypeName(varX) & vbTab & "Dereferenced Type=" & DereferencedType(varX)
Debug.Print vbTab & "str1: type=" & VarType(str1) & vbTab & vbTab & TypeName(str1) & vbTab & "Dereferenced Type=" & DereferencedType(str1)
Debug.Print
varX = lng1
Debug.Print "VarX is set equal to lng1:"
Debug.Print
Debug.Print vbTab & "varX: type=" & VarType(varX) & vbTab & vbTab & TypeName(varX) & vbTab & "Dereferenced Type=" & DereferencedType(varX)
Debug.Print vbTab & "lng1: type=" & VarType(lng1) & vbTab & vbTab & TypeName(lng1) & vbTab & "Dereferenced Type=" & DereferencedType(lng1)
Debug.Print
Set varX = ActiveSheet.Range("A1:C3")
Debug.Print "VarX is set equal to a range:"
Debug.Print
Debug.Print vbTab & "varX: type=" & VarType(varX) & vbTab & vbTab & TypeName(varX) & vbTab & "Dereferenced Type=" & DereferencedType(varX)
Debug.Print
Set rng1 = ActiveSheet.Range("A1:C3")
Set varX = Nothing
Set varX = rng1
Debug.Print "VarX is set equal to a range object variable:"
Debug.Print vbTab & "varX: type=" & VarType(varX) & vbTab & vbTab & TypeName(varX) & vbTab & "Dereferenced Type=" & DereferencedType(varX)
Debug.Print vbTab & "rng1: type=" & VarType(rng1) & vbTab & vbTab & TypeName(rng1) & vbTab & "Dereferenced Type=" & DereferencedType(rng1)
Debug.Print
arr1 = rng1.Value2
Set varX = Nothing
varX = arr1
Debug.Print "VarX is set equal to a range object's value, a 2-dimensional array:"
Debug.Print vbTab & "varX: type=" & VarType(varX) & vbTab & vbTab & TypeName(varX) & vbTab & "Dereferenced Type=" & DereferencedType(varX)
Debug.Print vbTab & "arr1: type=" & VarType(rng1) & vbTab & vbTab & TypeName(arr1) & vbTab & "Dereferenced Type=" & DereferencedType(arr1)
Debug.Print
Erase arr1
Debug.Print "The array variable is erased to Empty(). Inspect varX:"
Debug.Print vbTab & "varX: type=" & VarType(varX) & vbTab & vbTab & TypeName(varX) & vbTab & "Dereferenced Type=" & DereferencedType(varX)
Debug.Print vbTab & "arr1: type=" & VarType(rng1) & vbTab & vbTab & TypeName(arr1) & vbTab & "Dereferenced Type=" & DereferencedType(arr1)
Debug.Print
Set obj1 = ActiveSheet.Range("A1:C3")
Set varX = Nothing
Set varX = obj1
Debug.Print "VarX is set equal to an 'object' variable, which has been set to a range:"
Debug.Print vbTab & "varX: type=" & VarType(varX) & vbTab & vbTab & TypeName(varX) & vbTab & "Dereferenced Type=" & DereferencedType(varX)
Debug.Print vbTab & "obj1: type=" & VarType(rng1) & vbTab & vbTab & TypeName(obj1) & vbTab & "Dereferenced Type=" & DereferencedType(obj1)
Debug.Print
End Sub
结果:
Uninitialised: varX: type=0 Empty Dereferenced Type=0 str1: type=8 String Dereferenced Type=16392 lng1: type=3 Long Dereferenced Type=16387
varX and str1 are populated with the same literal: varX: type=8 String Dereferenced Type=8 str1: type=8 String Dereferenced Type=16392 lng1: type=3 Long Dereferenced Type=16387
varX and lng1 are populated with the same integer: varX: type=2 Integer Dereferenced Type=2 str1: type=8 String Dereferenced Type=16392 lng1: type=3 Long Dereferenced Type=16387
VarX is set equal to str1: varX: type=8 String Dereferenced Type=8 str1: type=8 String Dereferenced Type=16392
VarX is set equal to lng1: varX: type=3 Long Dereferenced Type=3 lng1: type=3 Long Dereferenced Type=16387
VarX is set equal to a range: varX: type=8204 Range Dereferenced Type=9
VarX is set equal to a range object variable: varX: type=8204 Range Dereferenced Type=9 rng1: type=8204 Range Dereferenced Type=16393
VarX is set equal to a range object's value, a 2-dimensional array: varX: type=8204 Variant() Dereferenced Type=8204 arr1: type=8204 Variant() Dereferenced Type=8204
The array variable is erased to Empty(). Inspect varX: varX: type=8204 Variant() Dereferenced Type=8204 arr1: type=8204 Variant() Dereferenced Type=8204
VarX is set equal to an 'object' variable, which has been set to a range: varX: type=8204 Range Dereferenced Type=9 obj1: type=8204 Range Dereferenced Type=16393
总而言之,一个有趣的问题:我的答案的简短版本是你可以消除变体和简单类型的歧义,但是一个被声明为' object'不适合该分析。
答案 1 :(得分:10)
您拥有的代码可以确定变量是否已为Variant
。现在您需要做的就是获取子类型,对吧?它具有内置功能:VarType。
但它有局限性。它仅适用于本机类型。对于用户定义的类型,它始终返回vbUserDefinedType
(36)。虽然,我想你可以通过调用TypeName
来完成这项工作。