Take text string from cell into VBA code and use it?

DJT80

New Member
Joined
Apr 12, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am trying to modify some VBA code I got from a YouTuber so that I can take the folder path that they enter and use it in the below code. I am a novice VBA'er so would greatly appreciate if someone can help me out with the correct coding.

VBA Code:
Sub ssFav()
  Dim objGetPath As String
  GetPath = InputBox("What’s the folder path that you want to search within?")
  Range("C3").Value = GetPath
End Sub

Sub ListAllFiles()

Dim objFSO As Scripting.FileSystemObject
'objFSO is the File System object
Dim objFile As Scripting.File
Dim objTargetPath As String

'Set objTargetPath = ???

Set objFSO = CreateObject("Scripting.FileSyestemObject")
Set objFolder = objFSO.GetFolder("TargetPath")
'need to input parent folder file path above

Call GetFileDetails(objFolder)


End Sub

Function GetFileDetails(objFolder As Scripting.Folder)


Dim objFolder As Scripting.Folder
Dim nextRow As Long
Dim objSubFolder As Scripting.Folder

nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
For Each objFile In objFolder.Files
    Cells(nextRow, 1) = objFile.Name
    Cells(nextRow, 2) = objFile.Path
    Cells(nextRow, 3) = objFile.Size
    Cells(nextRow, 4) = objFile.Type
    Cells(nextRow, 5) = objFile.DateCreated
    Cells(nextRow, 6) = objFile.DateLastModified
    nextRow = nextRow + 1
Next

For Each objSubFolder In objFolder.SubFolders
    Call GetFileDetails(objSubFolder)
Next

End Function
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
VBA Code:
Sub ssFav()
    Dim GetPath As String
    GetPath = InputBox("What’s the folder path that you want to search within?")
    If GetPath <> "" Then
        Range("C3").Value = GetPath
    End If
End Sub

Sub ListAllFiles()
    Dim objFSO As Object     'objFSO is the File System object
    Dim TargetPath As String
    Dim objFolder As Object
    
    TargetPath = Range("C3").Value  'Set objTargetPath = ???
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(TargetPath) Then
        Set objFolder = objFSO.GetFolder(TargetPath)
        Call GetFileDetails(objFolder)
    Else
        MsgBox "Folder '" & TargetPath & "' does not exist", vbCritical
    End If
End Sub

Sub GetFileDetails(objFolder)
    Dim objFile As Object
    Dim nextRow As Long
    Dim objSubFolder As Object
    
    nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
    For Each objFile In objFolder.Files
        Cells(nextRow, 1) = objFile.Name
        Cells(nextRow, 2) = objFile.Path
        Cells(nextRow, 3) = objFile.Size
        Cells(nextRow, 4) = objFile.Type
        Cells(nextRow, 5) = objFile.DateCreated
        Cells(nextRow, 6) = objFile.DateLastModified
        nextRow = nextRow + 1
    Next objFile
    
    For Each objSubFolder In objFolder.SubFolders
        Call GetFileDetails(objSubFolder)
    Next objSubFolder
End Sub
 
Upvote 0
VBA Code:
Sub ssFav()
    Dim GetPath As String
    GetPath = InputBox("What’s the folder path that you want to search within?")
    If GetPath <> "" Then
        Range("C3").Value = GetPath
    End If
End Sub

Sub ListAllFiles()
    Dim objFSO As Object     'objFSO is the File System object
    Dim TargetPath As String
    Dim objFolder As Object
   
    TargetPath = Range("C3").Value  'Set objTargetPath = ???
   
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(TargetPath) Then
        Set objFolder = objFSO.GetFolder(TargetPath)
        Call GetFileDetails(objFolder)
    Else
        MsgBox "Folder '" & TargetPath & "' does not exist", vbCritical
    End If
End Sub

Sub GetFileDetails(objFolder)
    Dim objFile As Object
    Dim nextRow As Long
    Dim objSubFolder As Object
   
    nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
    For Each objFile In objFolder.Files
        Cells(nextRow, 1) = objFile.Name
        Cells(nextRow, 2) = objFile.Path
        Cells(nextRow, 3) = objFile.Size
        Cells(nextRow, 4) = objFile.Type
        Cells(nextRow, 5) = objFile.DateCreated
        Cells(nextRow, 6) = objFile.DateLastModified
        nextRow = nextRow + 1
    Next objFile
   
    For Each objSubFolder In objFolder.SubFolders
        Call GetFileDetails(objSubFolder)
    Next objSubFolder
End Sub
Thank you! Very much appreciated!
 
Upvote 0

Forum statistics

Threads
1,215,703
Messages
6,126,314
Members
449,308
Latest member
Ronaldj

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top