如何基于单元格值在子文件夹中创建子文件夹 使用另一个值命名新文件夹

好的,我已经解决了这个问题,但仍然不确定我做错了什么。

Sub Dispatch_WO_Folder()
    If Dir(ThisWorkbook.Path) & "\" & Range("E10").Value,:=xlQualityStandard,MkDir Application.ThisWorkbook.Path & "\DISPATCHED WORK ORDERS\" & Range("B18") \ Range("E10").Value & ""
    End If
End Sub

Sub Dispatch_WO_PDF()
    activeSheet.ExportAsFixedFormat Type:=xlTypePDF,Filename:= _
        ThisWorkbook.Path & "\" & " & Range("E10").Value & ",Quality:=xlQualityStandard,_
        IncludeDocProperties:=True,IgnorePrintAreas:=False,OpenAfterPublish:=True
End Sub

如何基于单元格值在子文件夹中创建子文件夹 使用另一个值命名新文件夹

xuming_11 回答:如何基于单元格值在子文件夹中创建子文件夹 使用另一个值命名新文件夹

下面的代码演示了如何逐步构建完整的文件名并在每个阶段检查结果。

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

大家都在问