VBA根据需要将值隐式转换为Variant
,那么CVar
function的用例是什么?在example given in the documentation中,对CVar
的调用显然是多余的。
确切地说,我正在寻找一个最小的VBA代码示例,其中
CVar(...some expression...)
替换为...some expression...
时,不会编译(或产生不同的输出)。我无法找到这样的例子,但是也许其他人可以找到。
答案 0 :(得分:2)
我无法考虑以下实际用途,但至少表明您可以需要此功能:
Sub test()
Dim myInt As Integer
myInt = 2
' The following call will throw a runtime error in testSub
Call testSub(myInt)
' That's okay
Call testSub(CVar(myInt))
End Sub
Sub testSub(ByRef p As Variant)
Debug.Print "P: " & VarType(p)
p = "ABC"
End Sub
答案 1 :(得分:2)
不确定是否符合条件,但是我唯一想到的就是与As Any
中的Declare
的交互。
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, source As Any, ByVal Length As Long)
Sub Test()
Dim source As Long, dest As Long
source = 42
CopyMemory dest, CVar(source), 4
MsgBox dest
CopyMemory dest, source, 4
MsgBox dest
End Sub