title: VBA - selecting a folder and referencing it as the path for a separate code
body: <p>I'm able to use this code to select a folder:</p>
  
  <pre><code>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 &lt;&gt; -1 Then GoTo NextCode
      sItem = .SelectedItems(1)
  End With
  
  NextCode:
  GetFolder = sItem
  Set fldr = Nothing
  End Sub
  </code></pre>
  
  <p>I also have this code, that works when the folder path is hardcoded. Basically, it gives me a list of file names and file paths that I use later in a separate section. Currently I have the hardcoded folder path commented out and I'm trying to use the above code to select the folder each time so that it is more user friendly.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>However, I'm not sure how to get the two different code sets to work together. I'm guessing the only part I need to change is this: </p>
  
  <pre><code>Set objFolder = objFSO.GetFolder(ChooseFolder)
  </code></pre>
  
  <p>I have it as ChooseFolder which is the sub above for now but that is clearly not the way to go about it. I tried it with sItem as well but that doesn't seem to work.</p>