AutoFit Cell via VBA?

Restricted

Board Regular
Joined
Aug 9, 2014
Messages
76
How would you best go about auto fitting column width?

I am using a sub procedure to insert new row(s), contingent on the user's input, into a worksheet, however it would be nice to have the data to be completely visible after the row(s) has been inserted.
I suppose this also needs to be done every time a new row is inserted, rather than just once, blatantly because new data could be longer than previous data entered.

So far, I have thought to use this:
Columns().AutoFit

It also appears as though both of these are different in nature, how do I be judicious about which to use?

Columns("A:H").AutoFit
Range("A1:H1").Columns.AutoFit

I have also heard the use of the Excel VBA 'EntireRow' property, would this help in any way?
 
EntireColumn references against the addresses passed to the Range argument, it is not limited to a single column, the example above with lines
Code:
Range("A40:H40").Value = 1
Range("A1:D1").EntireColumn
I hoped demonstrated this, when ONLY columns A-D inclusive autofit (autosized on the screen) before you clicked "Ok" on the message box. Perhaps if you use F9 and run through the code, 1 line at a time, you'll have a better understanding of it?

Range("A1:H1") will be all cells inclusive between A1:H1
Range("A1,C1,D1") would be just cells A1,C1 and D1 so Range("A1,C1,D1").EntireColumn would only be columns A, C and D.

Got it, thanks!
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,215,491
Messages
6,125,107
Members
449,205
Latest member
ralemanygarcia

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