在VBA中使用这些标准函数时,是否有任何理由编写VBA.Replace()
、VBA.Instr()
而不仅仅是Replace()
、Instr()
?
我能想到的唯一原因是,当您在自定义模块中声明了具有相同名称的函数,并且希望对它们进行区分时。
编辑:由于下面提供的链接,这个例子得到了回答(简而言之,Len((是一个关键字,而不仅仅是一个函数(。这个例子很糟糕,因为Len((是个例外Debug.Print VBA.Len(10)
返回"2〃;而Debug.Print Len(10)
抛出错误。
最初的问题仍然存在,是否有任何不每次都使用VBA.
的警告?
您基本上回答了自己的问题:如果创建与VBA方法同名的公共方法,则它优先于VBA方法。例如,将这个放在一个模块中:
'------------------------------------------------------------------------------
'Purpose : Override the VBA.CreateObject function in order to register what object
' is being created in any error message that's generated.
'
' Author: Darin Higgins
' Source: http://www.darinhiggins.com/the-vb6-createobject-function/
'------------------------------------------------------------------------------
Public Function CreateObject(ByVal sClass As String, _
Optional ByVal sServerName As String = vbNullString _
) As Object
Dim sSource As String, sDescr As String, lErrNum As Long
On Error Resume Next
If Len(sServerName) Then
Set CreateObject = VBA.CreateObject(sClass, sServerName)
Else
Set CreateObject = VBA.CreateObject(sClass)
End If
If VBA.Err Then
sSource = VBA.Err.Source
sDescr = VBA.Err.Description
lErrNum = VBA.Err
sDescr = sDescr & " (ProgID: " & sClass
If Len(sServerName) Then
sDescr = sDescr & ". Instantiated on Server '" & sServerName & "'"
End If
sDescr = sDescr & ")"
On Error GoTo 0
VBA.Err.Raise lErrNum, sSource, sDescr
End If
On Error GoTo 0
End Function
逐步执行代码Set x = CreateObject("Object.Class")
将进入此函数,而不是VBA.CreateObject.