VBA - 选择一个文件夹并将其作为单独代码的路径引用

时间:2014-08-01 19:32:26

标签: excel-vba vba excel

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

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,但这似乎不起作用。

2 个答案:

答案 0 :(得分:2)

为了更好地解释我的评论,您已将ChooseFolder定义为Sub。 Subs不返回值。但是,当您执行此操作时,您将其用作函数:

Set objFolder = objFSO.GetFolder(ChooseFolder)

因为您将运行ChooseFolder的结果传递给FSO的GetFolder函数。

您需要做的是将ChooseFolder声明为函数。

基本上,将ChooseFolder Sub替换为:

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

然后它应该按照你的期望做。其余的代码都没问题。

答案 1 :(得分:0)

ChooseFolder()设为功能,然后引用它:

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