Forum Discussion

JosephAdedeji's avatar
JosephAdedeji
Copper Contributor
Apr 25, 2022
Solved

VBA Issue - Application.FileDialog return Nothing

Is there something wrong with the lines of code below? It results in the following error.

 

 

 

Sub CountFiles()
Dim xFolder As String
Dim xPath As String
Dim xCount As Long
Dim xFiDialog As FileDialog
Dim xFile As String
Set xFiDialog = Application.FileDialog(msoFileDialogFolderPicker)
If xFiDialog.Show = -1 Then
xFolder = xFiDialog.SelectedItems(1)
End If
If xFolder = "" Then Exit Sub
xPath = xFolder & "\*.xlsx"
xFile = Dir(xPath)
Do While xFile <> ""
xCount = xCount + 1
xFile = Dir()
Loop
MsgBox xCount & " files found"
End Sub

 

Resources