Why does my VBA code throw an “Invalid outside procedure” error?

Set statements aren’t allowed outside procedures. Move the Set statement into the Formatting procedure:

Sub Formatting()
    Set shtThisSheet = Application.Workbook("Formatting2.xlsm").Worksheets("Sheet1")
    ...

(I’d move the Dim statement into the procedure as well. I prefer to avoid global variables when possible.)

Leave a Comment