Description:
Some times we may enter the data into cells more than it’s width. In this case we can not able to see entire text. So we can change row height and Column width using excel using VBA. So that we can see entire data in that cell. When you have more lengthy data in cells, you can Auto Adjust Column Width or Row Height in Excel VBA to show the entire data. So that users can see the entire data in the cells. We will see with Examples.
Changing Row Height in Excel VBA
We can change row height in Excel using RowHeight Property of a Row in VBA. See the following example to do it.
Examples
The following example will change the height of the 3rd Row to 25.
Sub sbChangeRowHeight() 'Changing the 3rd row Height Rows(3).RowHeight = 25 End Sub
We can also set the height for multiple rows, the following example will change the height of the 3rd to 20th row height to 25.
Sub sbChangeRowHeightMulti() 'Changing the 3rd-25the row Height Rows("3:25").RowHeight = 25 End Sub
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a Module for Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Press F5 to execute itit
Changing Column Width in Excel VBA
We can change column width in Excel using ColumnWidth Property of a Column in VBA. See the following example to do it.
In this Example I am changing the Column B width to 25.
Sub sbChangeColumnWidth() Columns("B").ColumnWidth = 25 End Sub
Examples
We can also set the column width for multiple columns at a time, see this Example I am changing the Column B to E width to 25.
Sub sbChangeColumnWidthMulti() Columns("B:E").ColumnWidth = 25 End Sub
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a Module for Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Press F5 to execute it
Auto Adjust Column Width and Row Height using Excel VBA
We can use AutoFit method of Columns and Rows in Excel using VBA to Auto Adjust the rows and Columns.
Examples
Code to Auto Adjust Column Width
Following are the example to show you how to do this.
Sub sbAutoAdjustColumnWidth() Columns(2).AutoFit End Sub
Code to Auto fit Row Height
Following are the example to show you how to do this.
Sub sbAutoAdjustRowHight() Rows(2).AutoFit End Sub
Instructions:
Follow the instructions below to test the codes above.
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a Module for Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Press F5 to execute it
I wanted the row height for content of one column, regardsless what was in the other columns. I did it as follows:
Sub rowheight_one_column()
Column = InputBox(“Hoeveelste kolom?”) + 0
Rows(20).Delete
For x = 4 To 13
Cells(20, Column) = Cells(x, Column)
Rows(20).AutoFit
hoogte = Cells(20, Column).RowHeight
Rows(x).RowHeight = hoogte
Next x
Rows(20).Delete
End Sub
Please help me for auto fit the entire sheet1
where have you declared your Variables, It does confuse people when your code is not neat and is exposed on the Internet where everybody does search and get stuck.
the difference between writing the code in code window and a module?