I am not super good at VBA (my typical use cases are recording Macros, and cleaning and modifying VBA as opposed to creating anything from scratch). I'm trying to slim down ~300 excel workbooks before consolidating them all using Kutools.
I came up with a bit of vba to strip some unnecessary parts of these workbooks to enable my consolidation. This code works without issue when run on any of the workbooks individually:
Sub PrepWorkbook()
Dim Sh As Worksheet
For Each Sh In ThisWorkbook.Worksheets
If Sh.Visible = True Then
Sh.Activate
Sh.Cells.Copy
Sh.Range("A1").PasteSpecial Paste:=xlValues
Sh.Range("A1").Select
End If
Next Sh
Application.CutCopyMode = False
Dim ws As Worksheet
For Each ws In Worksheets
ws.Cells.Validation.Delete
Next ws
Application.DisplayAlerts=FALSE
Sheets("Instructions").Delete
Sheets("Dropdowns").Delete
Sheets("Dropdowns2").Delete
Sheets("Range Reference").Delete
Sheets("All Fields").Delete
Sheets("ExistingData").Delete
Application.DisplayAlerts=TRUE
End Sub
I found an excellent bit of code on stackoverflow that runs a predetermined task across multiple workbooks that I am tried adapting for my purposes:
Sub ProcessFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = ActiveWorkbook.Path & "\Files\"
Filename = Dir(Pathname & "*.xls")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
DoWork wb
wb.Close SaveChanges:=True
Filename = Dir()
Loop
End Sub
Sub DoWork(wb As Workbook)
With wb
'Do your work here
.Worksheets(1).Range("A1").Value = "Hello World!"
End With
End Sub
Original thread can be found here: Run same excel macro on multiple excel files
I've tried inserting my code into the the "'Do your work here" and ".Worksheets(1).Range("A1").Value = "Hello World!"" lines in the original vba, but have had no success. I've also tried similarly inserting my parsing code into a few other solutions to executing macros across multiple excel workbooks with no success.
The workbooks it calls upon are being opened and saved, but the actual work my code is trying to accomplish isn't happening (without logging an error). I suspect that a piece of the code I'm inserting is incompatible in a way that would be very obvious to someone more knowledgable than I am.
Can anyone offer some help/guidance here? I really just need code or direction on how to execute my original "PrepWorkbook" VBA on the 300 workbooks found in "C:\Temp\Workbooks"