我可以使用此代码来选择文件夹:

Sub ChooseFolder()
Dim fldr As FileDialog
Dim sItem As String

Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .InitialFileName = strPath
    If .Show <> -1 Then GoTo NextCode
    sItem = .SelectedItems(1)
End With

NextCode:
GetFolder = sItem
Set fldr = Nothing
End Sub

我也有这个代码,当文件夹路径被硬编码时它可以工作。基本上,它为我提供了文件名和文件路径的列表,我稍后将在单独的部分中使用它们。目前,我已注释掉硬编码的文件夹路径,并且我尝试每次使用上面的代码来选择文件夹,以便它对用户更加友好。

Private Sub btn_LeaveReport()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Get the folder object
'Set objFolder = objFSO.GetFolder("D:\Administration\Time Sheets")
Set objFolder = objFSO.GetFolder(ChooseFolder)
i = 3

'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
    'print file name
    Cells(i + 1, 2) = objFile.Name
    'print file path
    Cells(i + 1, 3) = objFile.Path
    i = i + 1
Next objFile
End Sub

但是,我不确定如何让两个不同的代码集一起工作。我猜我唯一需要改变的部分是:

Set objFolder = objFSO.GetFolder(ChooseFolder)

我将其设置为 ChooseFolder,它是上面的子目录,但这显然不是解决问题的方法。我也用 sItem 尝试过,但这似乎不起作用。

有帮助吗?

解决方案

只是为了在我的评论的基础上提供更好的解释,您已经定义了 ChooseFolder 作为替补。子进程不返回值。但是,当您执行以下操作时,您将其用作函数:

Set objFolder = objFSO.GetFolder(ChooseFolder)

因为你传递了运行的结果 ChooseFolder 至 FSO 的 GetFolder 功能。

你需要做的是声明 ChooseFolder 作为一个函数。

基本上,更换你的 ChooseFolder 子与此:

Function ChooseFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String

    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = strPath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With

NextCode:
    ChooseFolder = sItem
    Set fldr = Nothing
End Function

然后它应该做你期望的事情。你的其余代码都很好。

其他提示

使选择删除器()进入 function 然后引用它:

Public Function ChooseFolder()
    Dim fldr As FileDialog
    Dim sItem As String

    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = strPath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With

NextCode:
    ChooseFolder = sItem
    Set fldr = Nothing
End Function


Private Sub btn_LeaveReport()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim sFldr As String

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")

sFldr = ChooseFolder()
Set objFolder = objFSO.GetFolder(sFldr)
i = 3

'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
    'print file name
    Cells(i + 1, 2) = objFile.Name
    'print file path
    Cells(i + 1, 3) = objFile.Path
    i = i + 1
Next objFile
End Sub
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top