abbyn
abbyn

Reputation: 23

AutoFit all active rows / columns in a workbook

I want to create a macro autofit all rows and columns with values in them in across all worksheets in a workbook. This is what I have so far, I am stuck. Thank you in advance.

Sub AutoFitColumnsRows()
Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet
For Each ws In ThisWorkbook.Worksheets
    ws.Activate
     Cells.EntireRow.AutoFit
    Cells.EntireColumn.AutoFit
Next

starting_ws.Activate

End Sub

Upvotes: 0

Views: 1515

Answers (1)

braX
braX

Reputation: 11755

I am not sure why you need the other variables you have defined, but the basic loop looks like this:

For Each ws In ThisWorkbook.Worksheets
    ws.Cells.EntireRow.AutoFit
    ws.Cells.EntireColumn.AutoFit
Next

Upvotes: 3

Related Questions