I am trying to do AutoFit cells in the range A2 to AA2.
Below is the code I am trying:
Application.ScreenUpdating = False
Dim wkSt As String
Dim wkBk As Worksheet
wkSt = ActiveSheet.Name
For Each wkBk In ActiveWorkbook.Worksheets
On Error Resume Next
wkBk.Activate
Cells.Range("A2:AA2").EntireColumn.AutoFit
Next wkBk
Sheets(wkSt).Select
Application.ScreenUpdating = True
Below is some sample Excel data, as displayed on the screen:
A B C
1 Jhon is the best employee
2 Emp_Name Emp_id Empsal.
Note that “Jhon is the best employee” is all in one cell (A1) but when displayed its contents overlap cells B1 and C1.
With the above code, the data is then displayed as follows:
A B C
1 Jhon is the best employee
2 Emp_Name Emp_id Empsal
How can I do AutoFit on each cell of the second row in each worksheet? Above code failing to autofit data from my second row.
This is what I want the display to look like:
A B C
1 Jhon is the best employee
2 Emp_Name Emp_id Empsal.
i.e. the columns should fit snuggly around “Emp_Name”, “Emp_id” and “Empsal.” — not around “Jhon is the best employee”.
EDIT Also, if there are data in the following rows (3, 4, etc.) then these should be included in the Autofit as well. For example, data should display like below:
A B C
1 Jhon is the best employee
2 Emp_Name Emp_id Empsal.
3 Jhon Peter 2 2000
If what you want is to force Autofit to ignore the first row and only Autofit based on the content of rows 2 and greater. I don’t think this is possible.
One way to do it would be to store the content of the first row, delete it, autofit and insert the content of the first row back: