VBA中有时要判断文件或文件夹是否存在,为打开文件作准备,这里采用错误机制来判断 代码如下:Function 文件或文件夹是否存在(全路径 As String) As Boolean On Error GoTo EarlyExit If Not Dir(全路径, vbDirectory) = vbNullString Then 文件或文件夹是否存在 = True End If
Exit Function示例: vba Function SomeFunction() As String ' 执行一些操作 ' ... ' 如果满足某个条件,则提前退出并返回一个值 If someCondition Then SomeFunction = "Early Return" Exit Function End If ' 其他操作 ' ... SomeFunction = "Normal Return" End Function 使用错误处理机制: 如果需要在特...
EarlyExit:On Error GoTo 0 End Function 给你个简单的例子Sub test()Range("a1") = test2(3, 4)End SubFunction test2(x As Integer, y As Integer)test2 = x + yEnd Function运行test过程,A1单元格就会是7。function的参数必须要有值传递的
End IfEarlyExit:'整理 On Error Resume Next Set objFile=Nothing Set objFiles=Nothing Set objFso=Nothing On Error GoTo0End Function 可以使用下面的代码来测试: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 Subtest()Dim flDlg As FileDialog ...
Function getCmdlineOutput2(cmd As String) 'requires Reference: C:\Windows\System32\FM20.DLL (MS Forms 2.0) [Early Bound] Dim objClipboard As DataObject, strOrigClipbrd As Variant Set objClipboard = New MSForms.DataObject 'create clipboard object objClipboard.GetFromClipboard 'save existing clipb...
EarlyExit:On Error GoTo 0 End Function 将上述代码放入标准模块中,如果指定的文件夹或文件存在,FileFolderExists返回True。调用上述代码的方法:1.判断文件夹是否存在:Public Sub TestFolderExistence()If FileFolderExists("c:\windows\") Then MsgBox "指定的文件夹存在!"Else MsgBox "指定的文件夹...
4.判断指定文件或者文件夹是否存在 Public Function FileFolderExists(strFullPath As String) As Boolean On Error GoTo EarlyExit If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True EarlyExit: On Error GoTo 0 End Function...
本自定义函数由于使用了第三方库,使用前需要做Early Binding:即在VBE编辑器中,选择菜单栏中的Tool — Reference: 弹出如下图的对话框后,选择Microsoft VBSscript Regular Expression 5.5,打钩,点OK。 最后按Ctrl+S保存文件,注意在保存对话框中,文件类型需要选择“Excel启动宏的工作簿(*.xlsm)”,如下图 ...
EarlyExit: On Error GoTo 0 End Function 将上述代码放入标准模块中,如果指定的文件夹或文件存在,FileFolderExists返回True。调用上述代码的方法: 1.判断文件夹是否存在: Public Sub TestFolderExistence() If FileFolderExists("c:\windows\") Then MsgBox "指定的文件夹存在!" ...
If oDAO Is Nothing Then Exit Sub ‘Function Microsoft Excel XX.X Object Library EARLY BINDINGLATE BINDING REFERENCEMicrosoft Excel XX.X Object Library (excel.exe) EXAMPLEDim oExcel As Excel.Application Set oExcel = New Excel.Application