I've created a vba macro with the intention of: 1) Opening each file in a folder one by one 2) Loop through each worksheet, unprotect each sheet see if the top row is blank (and delete it if it is) and delete a problematic column. 3) Save the file as an xlsx.
So far I've managed to get it to loop through every file but fail to loop through the worksheets. I was previously able to get it to make changes to the last active worksheet in each workbook but now it seems to skip every worksheet.
Any idea why?
Sub LoopThroughFiles()
FolderName = ThisWorkbook.Path & "\Source Data\"
If Right(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator
Fname = Dir(FolderName & "*.xls*")
'loop through the files
Do While Len(Fname)
With Workbooks.Open(FolderName & Fname)
Dim folderPath As String
Dim filename As String
Dim wb As Workbook
Dim ws As Worksheet
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
'Unshare Workbook
If ActiveWorkbook.MultiUserEditing Then
ActiveWorkbook.ExclusiveAccess
End If
'Unprotect Workbook
ActiveWorkbook.Unprotect "pa55word"
For Each ws In ThisWorkbook.Worksheets
'Unprotect Worksheet
ws.Unprotect "pa55word"
'Unhide Columns and Rows
ws.Cells.EntireColumn.Hidden = False
ws.Cells.EntireRow.Hidden = False
'Delete Blank top Row
Set MR = ws.Range("A1:C1")
For Each cell In MR
If cell.Value = "" Then cell.EntireRow.Delete
Next
'Delete annoying Column
Set MR = ws.Range("A1:BZ1")
For Each cell In MR
If cell.Value = "a2a" Then cell.EntireColumn.Delete
Next
'Remove Filter
If ws.AutoFilterMode Then
ws.ShowAllData
ws.AutoFilterMode = False
End If
Next ws
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\Cleansed Data\" & Replace(Replace(ActiveWorkbook.Name, ".xlsx", ""), ".xls", "") & ".xlsx", FileFormat:=51
ActiveWorkbook.Close
End With
' go to the next file in the folder
Fname = Dir
Loop
End Sub