how to adapt from FolderPicker input to file path as variable?

jbaich

Board Regular
Joined
Nov 2, 2011
Messages
139
Hi all! I found this code online a while back and have been using it and it works great, but now I'd like to remove the user input from the process and instead derive the folder path from variables that were assigned earlier in the macro...

The previous version...
Code:
Sub GetFileNames()

Dim LastRow As Long
Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$

Range("A:B").ClearContents

InitialFoldr$ = "H:\"
With Application.FileDialog(msoFileDialogFolderPicker)  '//I would like to change this to something like... (FldrRoot & "\" & FldrLvl1 & "\" & FldrLvl2 & "\" & Year(Date) & " PVS")\\
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
Range("B1").Activate
ActiveCell.Offset(xRow) = xFname$
xRow = xRow + 1
xFname$ = Dir
Loop
End If
End With

So basically I just want to replace the folder picker with "CYPVS", which is a variable that I have assigned the file path too... and I can't seem to get it to work. Incase it's not clear from the code, the desired result is a list of all the file names (not paths) in the folder, starting in cell B2 and going down. Also, if anyone knows how to only list the .pdf files, that would be helpful!


Code:
Set IPDF = Sheets(Year(Date) - 1 & " PVS").OLEObjects.Add(Filename:=xDirect2015PVS$ & PDFRef, Link:=False, DisplayAsIcon:=False) '//Insert PDF, see Explanations sheet\\
    
    With IPDF
        .Top = Sheets(Year(Date) - 1 & " PVS").Range("A1").Top '//Set the top to be cell A1\\
        .Left = Sheets(Year(Date) - 1 & " PVS").Range("A1").Left '//Set the left to be cell A1\\
    End With

In this snippet the filename:=xDirect2015PVS would be replaced with filename:=CYPVS & PDFRef is the variable looping through the file names.

Thanks in advance!
Joe
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,215,525
Messages
6,125,325
Members
449,218
Latest member
Excel Master

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