Lifeseeker
Registered User.
- Local time
- Yesterday, 23:05
- Joined
- Mar 18, 2011
- Messages
- 273
Hi there,
I have some pieces of code that I am hoping to open a some excel.xlsm files in subfolders within a main folder to do some simple manipulations.
file path for the 1st excel file:
C:\Users\GJ\Desktop\TEST1\aaa.xlsx
file path for the 2nd excel file:
C:\Users\GJ\Desktop\TEST2\bbb.xlsx
The main directory stays the same, and the file structure is the same in TEST1 and TEST2 sub folders, but how do I loop through these 2 subfolders under Desktop?
Am I on the right track?
I have some pieces of code that I am hoping to open a some excel.xlsm files in subfolders within a main folder to do some simple manipulations.
file path for the 1st excel file:
C:\Users\GJ\Desktop\TEST1\aaa.xlsx
file path for the 2nd excel file:
C:\Users\GJ\Desktop\TEST2\bbb.xlsx
The main directory stays the same, and the file structure is the same in TEST1 and TEST2 sub folders, but how do I loop through these 2 subfolders under Desktop?
Code:
Sub Command0_Click()
Dim folderPath As String
Dim filename As String
Dim WB As Object
Dim xlApp As Object
Set xlApp = CreateObject(Excel.Application)
folderPath = "C:\Users\GJ\Desktop\"
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*.xlsx")
Do While filename <> ""
Set WB = xlApp.Workbooks.Open(folderPath & filename)
WB.Close False
filename = Dir
Loop
End Sub
Am I on the right track?