Excel VBA 函数:判断多个文件夹是否存在并停止判断
使用以下的 VBA 代码来实现判断多个文件夹是否存在并停止后续判断的功能:
Sub CheckFolders()
Dim folderPath1 As String
Dim folderPath2 As String
Dim folderPath3 As String
folderPath1 = 'C:\Folder1' '第一个文件夹路径
folderPath2 = 'C:\Folder2' '第二个文件夹路径
folderPath3 = 'C:\Folder3' '第三个文件夹路径
If Dir(folderPath1, vbDirectory) <> '' Then
MsgBox folderPath1 & ' 文件夹已存在'
ElseIf Dir(folderPath2, vbDirectory) <> '' Then
MsgBox folderPath2 & ' 文件夹已存在'
ElseIf Dir(folderPath3, vbDirectory) <> '' Then
MsgBox folderPath3 & ' 文件夹已存在'
Else
MsgBox '所有文件夹都不存在'
End If
End Sub
在这段代码中,我们首先定义了三个文件夹的路径。然后通过使用 Dir 函数来判断每个文件夹是否存在。如果第一个文件夹存在,则显示相应的消息并停止后面的文件夹判断。如果第一个文件夹不存在,则继续判断第二个文件夹,以此类推。
你可以根据实际的文件夹路径进行修改。
原文地址: https://www.cveoy.top/t/topic/jsKl 著作权归作者所有。请勿转载和采集!