【问题标题】:Changing path to parent directory of active working folder更改活动工作文件夹的父目录的路径
【发布时间】:2017-10-24 21:00:59
【问题描述】:

我有一个列出目录中文件的代码。如何寻址代码以查看当前工作簿目录的父目录?我希望它在我放置的任何地方都是独立的。 (第一个代码寻址到第二个读取文件) 谢谢。

....
      Application.ScreenUpdating = False
      ShowPDFs "C:\Test\Working\", ws
        ws.UsedRange.EntireColumn.AutoFit
    Application.ScreenUpdating = True
End Sub
------------------------------
Private Sub ShowPDFs(ByRef fsoPath.......

【问题讨论】:

  • 使用\..可以轻松获取目录的父目录。例如。如果您有一个包含路径"C:\Test\Working" 的变量(我们称之为myPath),那么myPath & "\.." 将是"C:\Test\Working\..",它等价于"C:\Test"。 (但我对这个问题的理解不足以弄清楚你想用这个事实做什么。)
  • 那么,如果这是 Excel VBA 代码,您想使用ThisWorkbook.Path & "\.." 作为使用的目录吗?因此,如果工作簿是 "X:\dir1\dir2\dir3\myBook.xlsm",它将在 "X:\dir1\dir2" 目录中查找。
  • createobject("scripting.filesystemobject").getfolder(thisworkbook.Path).parentfolder.path
  • @TimWilliams 更容易添加"\.." ;)(但你可以看出我正在显示我的年龄:D)
  • 同意 - 只是大声思考。 (同意容易不同意年龄...)

标签: vba directory parent


【解决方案1】:

只需检查文件不在根级别:

....
    Application.ScreenUpdating = False

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

        ws.UsedRange.EntireColumn.AutoFit

    Application.ScreenUpdating = True

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

【讨论】:

  • 我也更新了previous answer
  • par 设置为just "\.." 将意味着它指向当前驱动器的根目录。我认为您想使用par = par & IIf(Len(par) > 2, "\..", ""),以便它指向包含工作簿的目录的父目录(即ThisWorkbook.Path & "\..")或"C:""\\"。但是如果ThisWorkbook.Path只是"C:",那么你仍然可以使用"C:\.." OK,那么你也可以将整行替换为par = ThisWorkbook.Path & "\.."。 (我认为ThisWorkbook.Path 永远不会只是"\\",所以没有什么意义。)
  • @YowE3K - 好点 - 我更新了 par & "\.." 部分。我明天会检查网络路径,如果ThisWorkbook.Path 不能是"\\",我将删除所有"par" 行(但我认为我之前使用它来确定我是否正在处理网络位置)
  • 我无法想象一个不包含计算机名称的 UNC 路径(即,我认为文件名不可能为 \\myworkbook.xlsm),但如果 UNC 是\\mycomputer\myworkbook.xlsm 之类的东西 - 如果您尝试查看目录 \\mycomputer\..,可能会造成混淆。我会让你做一些测试。
【解决方案2】:

您想要的解决方案:

如果您的行为是打开一个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多