下面的代码演示了如何逐步构建完整的文件名并在每个阶段检查结果。
Sub Dispatch_WO_Folder()
' 302
Dim FldName As String
' 1. Create the name you want to search for before starting the search
' 2. don't refer to cells by their range names (too cumbersome)
FldName = Cells(10,"E").Value ' actually,it's Cells(10,5)
Debug.Print FldName ' check the name
FldName = Cells(18,"B").Value & "\" & FldName ' observe how to add the path separator
Debug.Print FldName ' check the name
FldName = ThisWorkbook.Path & "\DISPATCHED WORK ORDERS\" & FldName
Debug.Print FldName ' check the name
If Dir(FldName) = vbNullString Then MkDir FldName
End Sub
如果 E10 或 B18 为空白,则该尝试将失败。因此,这里是上述程序的稍微复杂的版本,可以避免此类事故。
Sub Dispatch_WO_Folder()
' 302
Dim Tmp As String
Dim FldName As String
' 1. Create the name you want to search for before starting the search
' 2. don't refer to cells by their range names (too cumbersome)
FldName = Cells(10,"E").Value ' actually,5)
Debug.Print FldName ' check the name
If Len(FldName) Then
Tmp = Cells(18,"B").Value
If Len(Tmp) Then
FldName = Tmp & "\" & FldName ' observe how to add the path separator
Debug.Print FldName ' check the name
FldName = ThisWorkbook.Path & "\DISPATCHED WORK ORDERS\" & FldName
Debug.Print FldName ' check the name
If Dir(FldName) = vbNullString Then MkDir FldName
Else
MsgBox "Cell B18 is blank"
End If
Else
MsgBox "Cell E10 is blank"
End If
End Sub
一旦您知道您的代码正确创建了文件夹名称,您就可以移除各种视觉检查。然而,自动检查并非如此,因为只要您使用代码,您就会希望它们保持有效。
本文链接:https://www.f2er.com/3900.html