VBA:更改 Excel 单元格宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22322550/
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
VBA: Change Excel cell width
提问by Celestial Dragon of Osiris
I have some VBA and batch Scripts that read the Mac Address Table out of some switches and import it into Excel to format it.
But the text is too long for the default cell width.
Is it possible to change the displayed cell width?
我有一些 VBA 和批处理脚本,可以从某些交换机中读取 Mac 地址表并将其导入 Excel 以对其进行格式化。
但是文本对于默认单元格宽度来说太长了。
是否可以更改显示的单元格宽度?
(When saying displayed cell width I mean: this)
(当说显示单元格宽度时,我的意思是:this)
回答by ttaaoossuu
Use this:
用这个:
Range("A1").ColumnWidth = ...
The units for this value are as following:
该值的单位如下:
One unit of column width is equal to the width of one character in the Normal style. For proportional fonts, the width of the character 0 (zero) is used.
一个单位的列宽等于一个普通样式中的一个字符的宽度。对于比例字体,使用字符 0(零)的宽度。
For example, the column width for freshly opened Excel file with default styles is 8.43
which is equal to 64 pixels.
例如,新打开的具有默认样式的 Excel 文件的列宽8.43
等于 64 像素。
...or this to autofit width:
...或者这是自动调整宽度:
Range("A1").EntireColumn.AutoFit
回答by Smittie
Another method would be:
另一种方法是:
.Columns("A").ColumnWidth = 20 (or whatever value you need)
I didn't compare it speedwise but why my guess would be that it's more efficient to just use Columns() instead of Range().
我没有比较它的速度,但为什么我的猜测是使用 Columns() 而不是 Range() 会更有效。
For more info on the ColumnWidth-Value -> MSDN Doc for the columnwidth-property
有关 ColumnWidth-Value -> MSDN Doc for the columnwidth-property 的更多信息