문제

할 수 있어요 이 코드를 사용하면 폴더를 선택합니다:

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 Sub this:

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

해 다음을 수행할 수 있습니다.의 나머지 부분의 코드입니다.

다른 팁

constolder () 함수 로 만들고 참조

를 참조하십시오.
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