So I've created a simple macro that loops through all worksheets and AutoFits all columns. It works when I run it manually but I want it to run each time I launch Excel automatically. I placed it in a module and named the sub Auto_Open()
. The problem is that I'm getting
"Run-time error '1004': Method 'Worksheets' of object '_Global' failed."
each time I start up Excel now.
Here's the code. The debugger says its the For Each
portion that is causing the problem but I don't understand why. What am I missing? Thanks for any help.
Sub AUTO_OPEN()
'
' AutoFit_All_Columns Macro
'
Dim ws As Worksheet
For Each ws In Worksheets
ws.UsedRange.Columns.AutoFit
Next ws
End Sub
UsedRange
as that could return an incorrect range - just usews.Columns.AutoFit
to reference all columns on the sheet (any blank columns shouldn't be affected). - Darren Bartrup-Cook