Home > Enterprise >  AutoFit all active rows / columns in a workbook
AutoFit all active rows / columns in a workbook

Time:08-10

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

CodePudding user response:

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
  • Related