将路径更改为活动工作文件夹的父目录 [英] Changing path to parent directory of active working folder

查看:244
本文介绍了将路径更改为活动工作文件夹的父目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a code that is list files in directory. how can I address code to look into parent directory of current workbook directory? i want it to be independent wherever I place it. (first code addressing to the second one to read file from) Thanks.

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


推荐答案

您想要的解决方案:

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

因为Excel总是以默认目录开头,即使它只是打开您最近的文件!

Cause the Excel always start with the default Directory even it's just open your recent file !

然后,这些应该会有所帮助。

Then, these should be help.

解决方案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

这篇关于将路径更改为活动工作文件夹的父目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆