excel VBA - loop through files in each sub-folder and start with a specific file

RCBricker

Well-known Member
Joined
Feb 4, 2003
Messages
1,560
Hello everyone,

I have a new project. I need to write a program that does the following:

1. User selects a folder
2. Code loops through the sub-folders of selected folder
3. Code loops through the files in the current sub-folder
4. Create a new excel file
5. starting with the "headerinfo" file
5A. import text into the new excel file
5B. loop through all of the remaining files in the sub-folder and import the text into excel file beneath the header info
6. save the new workbook to a specific file.

I began writing the following and then looked on line for the code further down.

Code:
Option Explicit

'for each file in folder
'   import to excel
'       start with header file
'       all other folders
'   Name each file
'   save in sub-folder
'   save on J:\ drive
'close new excel file
Public wbNEW As Workbook
Public wsSYS As Worksheet
Sub addwb()


Dim wb As Workbook, wbNEW As Workbook
Dim wsBUTTON As Worksheet, ws As Worksheet, wsSYS As Worksheet
Dim lngROW As Long, lngCOL As Long
Dim cell As Range, rng As Range
Dim varI As Variant, varJ As Variant
Dim strFOLDER As String, str As String

Set wb = ThisWorkbook
Set wsBUTTON = wb.Sheets("Button")

With Application.FileDialog(msoFileDialogFolderPicker)
   .AllowMultiSelect = False
   .Show
   On Error Resume Next
   strFOLDER = .SelectedItems(1)
   Err.Clear
   On Error GoTo 0
 End With
Call loopAllSubFolderSelectStartDirector(strFOLDER)

End Sub

I found the following code online, however I am not certain this is what I want and not sure how to change it to do steps 4-6.

Code:
Sub loopAllSubFolderSelectStartDirector(strFOLDER As String)

'Another Macro must call LoopAllSubFolders Macro to start to procedure
Call LoopAllSubFolders(strFOLDER)

End Sub
'List all files in sub folders
Sub LoopAllSubFolders(ByVal folderPath As String)

Dim fileName As String
Dim fullFilePath As String
Dim numFolders As Long
Dim folders() As String
Dim i As Long

If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
fileName = Dir(folderPath & "*.*", vbDirectory)

While Len(fileName) <> 0

    If Left(fileName, 1) <> "." Then
 
        fullFilePath = folderPath & fileName
 
        If (GetAttr(fullFilePath) And vbDirectory) = vbDirectory Then
            ReDim Preserve folders(0 To numFolders) As String
            folders(numFolders) = fullFilePath
            numFolders = numFolders + 1
        Else
            'Insert the actions to be performed on each file
'This example will print the full file path to the immediate window
Debug.Print folderPath & fileName
        End If
 
    End If
 
    fileName = Dir()

Wend

For i = 0 To numFolders - 1

    LoopAllSubFolders folders(i)
 
Next i

End Sub

I have never tried to do something like this so not certain how to proceed.

any help would be appreciated.

Thanks,
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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