vba - skip the loop on ThisWorkbook's folder

noidea23

New Member
Joined
Feb 16, 2022
Messages
28
Office Version
  1. 2021
Platform
  1. Windows
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
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Name will return the name of a file (ex: workbook.xlsm)

FullName will return the path and name of a file (ex: c:\myDocuments\workbook.xlsm)

Path will return the path of a file (ex: c:\myDocuments)

thisworkbook.name will return a filename AND the extension (.xlsm) so you are trying to compare that to a folder name and it wont match.
you can tyr one of the other extensions above to match the the PATH to your subfolder path or if the subfolder has exactly the same name as your workbook less the file extension you could use
VBA Code:
left(ThisWorkbook.name, Len(ThisWorkbook.name) - 5)
change 5 if needs be to the number of characters in your file extension including the "."
 
Upvote 0
Solution
Name will return the name of a file (ex: workbook.xlsm)

FullName will return the path and name of a file (ex: c:\myDocuments\workbook.xlsm)

Path will return the path of a file (ex: c:\myDocuments)

thisworkbook.name will return a filename AND the extension (.xlsm) so you are trying to compare that to a folder name and it wont match.
you can tyr one of the other extensions above to match the the PATH to your subfolder path or if the subfolder has exactly the same name as your workbook less the file extension you could use
VBA Code:
left(ThisWorkbook.name, Len(ThisWorkbook.name) - 5)
change 5 if needs be to the number of characters in your file extension including the "."

i see..thank you, ^ worked :)
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,083
Members
453,021
Latest member
Justyna P

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