更改活动工作文件夹的父目录的路径

时间:2017-10-24 21:00:59

标签: vba directory parent

我有一个代码是目录中的列表文件。如何解决代码以查看当前工作簿目录的父目录?无论我把它放在哪里,我希望它是独立的。 (第一个代码寻址到第二个读取文件) 感谢。

sum by (client)(rate(sniffer_requests_total[1m])

2 个答案:

答案 0 :(得分:0)

检查文件是否不在根级别:

....
    Application.ScreenUpdating = False

        ShowPDFs ThisWorkbook.Path & "\..", ws

        ws.UsedRange.EntireColumn.AutoFit

    Application.ScreenUpdating = True

End Sub
------------------------------
Private Sub ShowPDFs(ByRef fsoPath.......

答案 1 :(得分:0)

您想要的解决方案:

如果您的行为是打开excel窗口,然后打开您的最新文件,请注意,您不要忘记添加更改驱动器,然后将目录更改为VBA代码。

因为Excel总是打开默认文件,所以它总是以默认目录开头!

然后,这些应该有帮助。

解决方案A: 您无需获取父目录即可执行其他任何操作。

Dim ThisWorkbookPath As String
Dim ThisWorkbookPathParts As Variant

ThisWorkbookPath = ThisWorkbook.Path
ThisWorkbookPathParts = Split(ThisWorkbookPath, _
                        Application.PathSeparator)

ChDrive ThisWorkbookPathParts(LBound(ThisWorkbookPathParts))
ChDir ThisWorkbookPath

解决方案B: 您可能需要获取父目录才能执行其他操作。

Dim ParentPath As String: ParentPath = "\"
Dim ThisWorkbookPath As String
Dim ThisWorkbookPathParts, Part As Variant
Dim Count, Parts As Long

ThisWorkbookPath = ThisWorkbook.Path
ThisWorkbookPathParts = Split(ThisWorkbookPath, _
                        Application.PathSeparator)

Parts = UBound(ThisWorkbookPathParts)
Count = 0
For Each Part In ThisWorkbookPathParts
    If Count > 0 Then
        ParentPath = ParentPath & Part & "\"
    End If
    Count = Count + 1
    If Count = Parts Then Exit For
Next

MsgBox "File-Drive = " & ThisWorkbookPathParts _
       (LBound(ThisWorkbookPathParts))
MsgBox "Parent-Path = " & ParentPath