如何使用自动调整特定表列来包装文本?

huangapple go评论60阅读模式
英文:

How to wrap text with auto fit specific table columns?

问题

以下代码用于包装文本并自适应第二和第三列。然而,最后出现了运行时错误 424 "需要对象"。

Set ws = ActiveSheet
Set tbl = ws.ListObjects(2)

With ws
    tbl.ListColumns(2).Range.WrapText = True
    tbl.ListColumns(2).Range.Rows.AutoFit = True
    tbl.ListColumns(3).Range.WrapText = True
    tbl.ListColumns(3).Range.Rows.AutoFit = True
End With
英文:

The following code works to wrap text and autofit the 2nd and 3rd column. However, I get a runtime error 424 "object required" at the end.

Set ws = ActiveSheet
Set tbl = ws.ListObjects(2)

With ws
    tbl.ListColumns(2).Range.WrapText = True
    tbl.ListColumns(2).Range.Rows.AutoFit = True
    tbl.ListColumns(3).Range.WrapText = True
    tbl.ListColumns(3).Range.Rows.AutoFit = True
End With

</details>


# 答案1
**得分**: 1

[`AutoFit`](https://learn.microsoft.com/en-us/office/vba/api/excel.range.autofit) 是一个方法;移除 `= True`:

```vba
tbl.ListColumns(2).Range.Rows.AutoFit
...
tbl.ListColumns(3).Range.Rows.AutoFit 
英文:

AutoFit is a method; remove the = True:

tbl.ListColumns(2).Range.Rows.AutoFit
...
tbl.ListColumns(3).Range.Rows.AutoFit 

huangapple
  • 本文由 发表于 2023年7月28日 02:15:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定