Autofit Rows in VBA

JessP

New Member
Joined
Jan 11, 2018
Messages
23
Hello,

I have a spreadsheet that includes a few columns with a lot of text. To autofit the rows without taking those into account, I wrote the following code, which copies the first 11 columns of each row to row 2001, autofits it, then takes the row height and applies it to the original row. Rather roundabout, I know, but I couldn't come up with something more efficient.

However, it's resizing all of the rows each time rather than saving each one's height individually, so they all end up the same height as the last row. Does anyone have an idea as to how I can change the code to make that happen? Thanks!

Code:
Dim N as long
Dim I as long

N = Sheets("New FNA").Cells(Rows.Count, "B").End(xlUp).Row

For i = 4 To N
    Range(Cells(i, 1), Cells(i, 11)).Copy Destination:=Range("A2001")
    Range("A2001").EntireRow.AutoFit
    Range("i:i").RowHeight = Range("A2001").RowHeight
Next i
Range("A2001").EntireRow.Clear
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Which rows do you want to autofit?

By the way, this will set the RowHeight property of every row in column I.
Code:
Range("i:i").RowHeight = Range("A2001").RowHeight

If you wanted to set the row height of row i try this.
Code:
Rows(i).RowHeight = Range("A2001").RowHeight
 
Upvote 0
Beaten 2 it
 
Last edited:
Upvote 0
Thanks Norie (and Fluff)! Works perfectly - I should probably be more careful how I go about using single letter variables.
 
Upvote 0
I tend to avoid them, partly because they can cause problems as you've just discovered, but also if you need to search for them it can be time consuming & inaccurate.
For instance if you use the Find command (ctrl F) in the VBE if match case is off it will find every instance of i in your code (~14), but if you have Match Case on then it won't find the pair of i's in your Range("i:i")
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top