gpt4 book ai didi

excel - 给定一个固定的单元格宽度,我可以更改字体大小以使文本适合单元格吗?

转载 作者:行者123 更新时间:2023-12-04 21:41:27 27 4
gpt4 key购买 nike

我有代码可以自动调整单元格的高度,以便文本适合该单元格。
但是现在我想更改单元格内文本的字体大小,以便字体会缩小和增长以使文本适合单元格内,因此我不必将单元格宽度设为固定大小。

我如何在 VBA 中做到这一点?

给定字体大小,我需要知道文本的宽度

选项1。

TextWidth = WidthOfAText(Range("A1").Value)
FontAdjustmentFactor = Range("A1").EntireColumn.ColumnWidth / TextWidth
FontSize = Range("A1").Font.Size * FontAdjustmentFactor
Range("A1").Font.Size = Max(4, Min(10, FontSize))

或者我需要知道文本是否会修复并执行某种试错程序。

选项 2。
Range("A1").Font.Size = 10
While (Text_does_not_fit AND Range("A1").Font.Size >= 5)
Range("A1").Font.Size = Range("A1").Font.Size - 1
Wend

我该怎么做呢? (最好使用选项 1 中的代码)

最佳答案

非编程方式只需右键单击单元格 -> 格式化单元格 -> 对齐 -> 缩小以适合

明智的代码是:

Sheet1.Range("A1").ShrinkToFit = True

关于excel - 给定一个固定的单元格宽度,我可以更改字体大小以使文本适合单元格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7580727/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com