Passing Variables when using the Call Function

jsuvman58

New Member
Joined
Jan 14, 2012
Messages
17
I created a macro to look for specific file types (file extensions) and list them in the sheet. I created one but it do not do the subfolders. Found this one online and it worked well. I modified it to fit my needs.

I am not familiar with the “Call” Function but do know what it does. After two days of looking and reading and cannot figure something out and need some help. As you see from the macro it is currently set up just to search for “xlsx” files. I need to change that to a variable that I can enter in an InputBox.

However, the InputBox appears to have to be in the Sub MainList() before the “Call” Function?

If you try to put it in the other Sub ListFilesInFolder(…..) it will except that value but the value gets wiped out after the first folder is searched so the Input Box will just keep popping up with every subfolder. So someplace in the routine the value from the InputBox gets reset.

So this brings me to my question. If I put the InputBox in the Sub MainList() how do I pass that value to the other Sub so it can be used as a variable in place of the “xlsx”.

Example: If I do the below, the iBox variable does not get passed onto the other Sub from the Call Function.

Or is there a different way to do this?

Sub MainList()

Set folder = Application.FileDialog(msoFileDialogFolderPicker)

If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)

iBox = InputBox (“Enter File Extension”)

Call ListFilesInFolder(xDir, True)

End Sub

Below is the actual macro.
VBA Code:
Sub MainList()

Set folder = Application.FileDialog(msoFileDialogFolderPicker)

If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)

Call ListFilesInFolder(xDir, True)

End Sub
-------------------------------------------------------------------------------------------------------------
Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)

Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long

Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(xFolderName)
rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1

For Each xFile In xFolder.Files

strFileExt = xFileSystemObject.GetExtensionName(xFile)

If strFileExt = "xlsx" Then
Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
rowIndex = rowIndex + 1

End If

Next xFile

If xIsSubfolders Then

For Each xSubFolder In xFolder.subfolders
ListFilesInFolder xSubFolder.Path, True

Next xSubFolder

End If

Set xFile = Nothing
Set xFolder = Nothing
Set xFileSystemObject = Nothing

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,216,008
Messages
6,128,256
Members
449,435
Latest member
Jahmia0616

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