hiii! i'm currently working on looping subfolders to retrieve data from the spreadsheets in each subfolder. However, the masterlist spreadsheet (aka thisworkbook) is in one of the subfolders. I need help on asking vba to skip the subfolder that contains thisworkbook. What i did was "If subfolder.Name <> ThisWorkbook.Name Then .....continue but it didn't work. I have also pasted the codes below. Your help will be greatly appreciated!
Sub Subfolderloop()
Dim fso As Object
Dim wb As Object
Dim folder As Object
Dim subfolder As Object
Dim MyPath As String
Dim MyFile As String
Dim FdrPicker As FileDialog
Dim wba As Workbook
Dim wbn As String
Dim cell
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Set FdrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FdrPicker
.Title = "Select a Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
MyPath = .SelectedItems(1) & "\"
End With
NextCode:
MyPath = MyPath
If MyPath = "" Then GoTo ResetSettings Else
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.getfolder(MyPath)
For Each subfolder In folder.subfolders
If subfolder.Name <> ThisWorkbook.Name Then 'to skip this workbook's folder
For Each wb In subfolder.Files
If fso.GetExtensionName(wb.Path) = "xlsm" Then
wbn = fso.GetAbsolutePathName(wb)
Set wba = Workbooks.Open(Filename:=wbn)
On Error Resume Next
ActiveWorkbook.Unprotect Password:="abc"
ActiveWorkbook.Unprotect Password:="def"
ActiveWorkbook.Sheets("Sheet1").Visible = True
ActiveWorkbook.Sheets("Sheet1").Activate
Range("A2:F7").Copy
For Each cell In Workbooks("222").Worksheets("Sheet1").Columns(1).Cells
If IsEmpty(cell) = True Then
cell.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Exit For
Else
End If
Next cell
wba.Close False
End If
Next wb
End If
Next subfolder
ResetSettings:
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
Sub Subfolderloop()
Dim fso As Object
Dim wb As Object
Dim folder As Object
Dim subfolder As Object
Dim MyPath As String
Dim MyFile As String
Dim FdrPicker As FileDialog
Dim wba As Workbook
Dim wbn As String
Dim cell
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Set FdrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FdrPicker
.Title = "Select a Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
MyPath = .SelectedItems(1) & "\"
End With
NextCode:
MyPath = MyPath
If MyPath = "" Then GoTo ResetSettings Else
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.getfolder(MyPath)
For Each subfolder In folder.subfolders
If subfolder.Name <> ThisWorkbook.Name Then 'to skip this workbook's folder
For Each wb In subfolder.Files
If fso.GetExtensionName(wb.Path) = "xlsm" Then
wbn = fso.GetAbsolutePathName(wb)
Set wba = Workbooks.Open(Filename:=wbn)
On Error Resume Next
ActiveWorkbook.Unprotect Password:="abc"
ActiveWorkbook.Unprotect Password:="def"
ActiveWorkbook.Sheets("Sheet1").Visible = True
ActiveWorkbook.Sheets("Sheet1").Activate
Range("A2:F7").Copy
For Each cell In Workbooks("222").Worksheets("Sheet1").Columns(1).Cells
If IsEmpty(cell) = True Then
cell.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Exit For
Else
End If
Next cell
wba.Close False
End If
Next wb
End If
Next subfolder
ResetSettings:
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub