Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel / VBA: auto-adjust column width after pasting data

I have a blank Excel table saved as a template into which I copy data from the clipboard (originating from a website).

Is there a way using VBA to auto-adjust the width of each column to its content after the data (text) has been pasted in ?

Edit: The data I paste will always be inserted starting from A1 and will always fill the same number of columns.

like image 698
user2571510 Avatar asked Jun 05 '14 11:06

user2571510


People also ask

How do you make Excel Cells expand to fit text automatically VBA?

Press one of the following keyboard shortcuts: To AutoFit column width: Alt + H, then O, and then I. To AutoFit row height: Alt + H, then O, and then A.

How do you make Cells automatically resize in Excel?

Select cells you want to auto resize or CTRL + a for all cells. Menu Home tab -> Format->Column->AutoFit Row/Column Height.


2 Answers

You can do it with AutoFit:

 Columns("A:B").EntireColumn.AutoFit
like image 70
InformatikBabo Avatar answered Sep 17 '22 21:09

InformatikBabo


You can perform the following:

Columns("A").Autofit
like image 32
Lê Tuấn Thành Avatar answered Sep 18 '22 21:09

Lê Tuấn Thành