PDA

View Full Version : [SOLVED:] Macro Codes for auto fit columns and rows



Ahmed_Oman
06-13-2023, 12:58 PM
Hi,

I am new in VBA

What is the macro codes for auto fit columns and rows

Aussiebear
06-13-2023, 03:53 PM
Welcome to VBAX Ahmed_Oman. This opens up a wide variety of options for you. The basics would be derived around the following concepts

A particular Column (in this case Column A)

Column(1).EntireColumn.Autofit

A particular Row (in this case Row 3)

Row(3).EntireRow.Autofit

Multi Columns

Range("A:C").EntireColumn.Autofit


Multiple Rows

Rows("5:10").EntireRows.Autofit

Entire Sheet

Sub Autofit_Sheet()
With Worksheets("Sheet1").cells
.EntireColumn.Autofit
.EntireRow.Autofit
End With
End Sub

UsedRange

Sub AutofitURange()
Dim x as Integer
For x = 1 to Activesheet.UsedRange.Columns.Count
Column(x).EntireColumn.Autofit
Next x
End Sub


Really depends on what you are searching for I guess.

p45cal
06-13-2023, 04:31 PM
You can also restrict the column width autofit to a specific range:
Range("M4:M15").columns.autofit

It doesn't seem to work so well with rows

Ahmed_Oman
06-13-2023, 08:59 PM
thank you for the help

Aussiebear
06-13-2023, 11:34 PM
If this matter is resolved can you please use the Thread Tools option and select "Mark thread as Solved" please?

Aussiebear
06-15-2023, 02:13 PM
Don't you just love it when people just disappear.... without tidying up after themselves.