VBA - MS Excel, modify code to include files in sub-folders as well

vancouverjackluo

New Member
Joined
Jul 28, 2015
Messages
2
I have the following VBA macro, and I am using it to create a log by extracting certain values from a list of excel files in a particular.
However, instead of just the files in <code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; background-color: rgb(238, 238, 238);">G:\INSPECTION REPORTS\</code>, I would like it to work for ALL files in the sub-folders as well. For example, files in <code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; background-color: rgb(238, 238, 238);">G:\INSPECTION REPORTS\AAA</code> and <code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; background-color: rgb(238, 238, 238);">G:\INSPECTION REPORTS\BBB</code>, .....etc. How can I modify the following code to do so?


<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">Sub ABC()
Dim sPath As String, sName As String
Dim bk As Workbook, sh As Worksheet
Dim rw As Long

Set sh = ActiveSheet

rw
= 2 ' which row to write to in the activesheet
sPath
= "G:\INSPECTION REPORTS\" ' Path for file location
sName
= Dir(sPath & "*.xlsx") ' for xl2010 & "*.xlsx?"
Do While sName <> "" 'Loop until filename is blank
Set bk = Workbooks.Open(sPath & sName)
sh
.Cells(rw, "A") = bk.Name
sh
.Cells(rw, "B") = bk.Worksheets(1).Range("B8")
sh
.Cells(rw, "C") = bk.Worksheets(1).Range("B12")


rw
= rw + 1
bk
.Close SaveChanges:=False
sName
= Dir()
Loop

End Sub</code>
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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