Home » excel » vba – AutoFit column size in Excel based on content of column

vba – AutoFit column size in Excel based on content of column

Posted by: admin April 23, 2020 Leave a comment

Questions:

I would like to use a VBA code which I can run for all worksheets in a workbook to resize the columns in the following sequence:

1.) First, resize all columns in all worksheets to width 10

2.) Secondly, identify any column which have more TEXT than NUMBERS and Autosize the column to fit the contents.

The purpose of the changes is to provide a standard column width layout for our financial reports which get populated in Excel via Hyperion Smart View. All cells come out formatted as ‘Custom’ in Excel and cannot be changed. The report data (actual numbers) could start on any row within the spreadhseet, as the column titles sometimes take between 3 to 10 rows (maybe more). We have a lot of butterfly reports (dimensions in the middle ‘say column c’, with facts (numbers) to the left and right of that column). In some cases a column titled ‘Actuals’ in column A might have a sub-heading ‘Revenue’ further down (Row 50) in the same column. Therefore it’s fair to say that there is a mix of numbers and text in columns with no set rule.

Please help with the above or suggest any alternatives to achieve a code which can ensure that the number columns are all the same width (10) and the text column is ‘Autofit’ based on the size.

So far I have the below, which updates all column width to size 10 and then manually identifies the columns from worksheet BM03 and BM10 for AutoFit:

Sub Run_Me_To_Fix_Columns()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        Call resizingColumns(ws)

    If ws.name = "BM03a EBIT Report (Summary & De" Then
             ws.Columns("I:I").EntireColumn.Autofit

    ElseIf ws.name = "BM10 Balance Sheet" Then
             ws.Columns("A:A").EntireColumn.Autofit

         End If
    Next
End Sub

Sub resizingColumns(ws As Worksheet)
    With ws
        .Range("A:X").ColumnWidth = 10
            End With
End Sub
How to&Answers:

You could try to evaluate each column in each worksheet and compare the number of text cells with the number of numeric cells. Then format to autofit those that have more text than numbers.

    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        Call resizingColumns(ws)

'    If ws.Name = "BM03a EBIT Report (Summary & De" Then
'             ws.Columns("I:I").EntireColumn.AutoFit
'
'    ElseIf ws.Name = "BM10 Balance Sheet" Then
'             ws.Columns("A:A").EntireColumn.AutoFit
'
'         End If
    Next
End Sub

Sub resizingColumns(ws As Worksheet)
    With ws
        .Range("A:X").ColumnWidth = 10
    End With
    For i = 1 To 24
        Numbers = WorksheetFunction.Count(ws.Columns(i))
        Text = WorksheetFunction.CountA(ws.Columns(i)) - Numbers
        If Numbers < Text Then
            ws.Columns(i).EntireColumn.AutoFit
        End If
    Next i
    ws.Range("A1").Select

End Sub