Search code examples
excelexcel-2010excel-2007vba

VBA to select columns with headers in excel (How to auto-fit columns with data?)


Using VBA how can I select all the columns with headers in Excel? or all the columns which are not blank? Basically select all the columns with data in them.


Solution

  • Autofit columns with data

    Sub AutoFit()
        Rows("1:1").SpecialCells(xlCellTypeConstants, 23).Columns.AutoFit
    End Sub
    

    Or possibly

    Sub AutoFitCell()
      Cells.SpecialCells(xlCellTypeConstants, 23).Columns.AutoFit
    End Sub