Excel / VBA:粘贴数据后自动调整列宽

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24058774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:17:14  来源:igfitidea点击:

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

excelvbacolumn-width

提问by user2571510

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

我有一个空白的 Excel 表格保存为模板,我将剪贴板中的数据(来自网站)复制到其中。

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 ?

在粘贴数据(文本)后,有没有办法使用 VBA 将每列的宽度自动调整为其内容?

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

编辑:我粘贴的数据将始终从 A1 开始插入,并将始终填充相同数量的列。

回答by InformatikBabo

You can do it with AutoFit. Like this

您可以使用 AutoFit 做到这一点。像这样

Columns("A:B").Select

Selection.EntireColumn.AutoFit
Columns("A:B").Select

Selection.EntireColumn.AutoFit

回答by Lê Tu?n Thành

You can perform the following:

您可以执行以下操作:

Columns("A").Autofit