Re-code existing VBA

12Rev79

New Member
Joined
Mar 2, 2021
Messages
40
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi Experts,

Currently this code below will give me a list of all PDF only, I want to modify the code;

1. Allow me to select a folder
2. Will list all files in the folder selected could be PDF, .xlsx, .xls, .doc. .docx or Folder name and etc

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

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder("C:\Users\019\Desktop\DRAWING\COPY DRAWINGS")
i = 1
For Each objFile In objFolder.Files
Cells(i + 2, 2) = objFile.Name
Cells(i + 2, 4) = objFile.Path
i = i + 1
Next objFile

End Sub


Can anyone is kind to help me figure out the code to modify.

Thanks in advance,
12Rev79
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
try this:
VBA Code:
Sub OldName_Click()
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:
Set fldr = Nothing
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(sItem)
i = 1
For Each objFile In objFolder.Files
Cells(i + 2, 2) = objFile.Name
Cells(i + 2, 4) = objFile.Path
i = i + 1
Next objFile

End Sub
 
Upvote 0
Dear offthelip, thank you I appreciated so much of your help.
But the number 2 step is I think not yet included in the code, sorry if maybe I'm not cleat to that??
What code to add to list also the FOLDER NAME after selecting a folder in directory?
Let say in a selected directory there are mix files .docx, pdf, excel and a Folder.

1618200773877.png

So I need your help on how to modify the code to include in the list the Folder Names as will

Thank you in advance.

12Rev79
 
Upvote 0
Sorry I missed that detail, try this:
VBA Code:
Sub OldName_Click()
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:
Set fldr = Nothing
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim mysub As Object
Dim i As Integer

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(sItem)
i = 1
For Each objFile In objFolder.Files
Cells(i + 2, 2) = objFile.Name
Cells(i + 2, 4) = objFile.Path
i = i + 1
Next objFile

For Each mysub In objFolder.SubFolders
Cells(i + 2, 2) = mysub.Name
Cells(i + 2, 4) = mysub.Path
i = i + 1
Next mysub
 
End Sub
 
Upvote 0
Solution
Thank you offthelip for your kind help appreciated so much, it works will.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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