查找太小而无法显示内容的文本单元格
我有一个工作表,其中的单元格可能包含有时超出单元格宽度和高度的文本。如果我没有注意到它并调整行高,文本将显示为截止。有谁有可以定位这些单元格以便我调整它们的 VBA 片段或工具吗?谢谢!
I have a worksheet with cells that may contain text that sometimes exceeds the width and height of the cell. If I don't notice it and adjust the row height, the text shows up as cutoff. Does anyone have a VBA snippet or tool that can locate these cells so I can adjust them? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确定哪一列太宽会很棘手,特别是因为单元格内的文本可以具有不同的字体和/或字体大小。使用 Range.AutoFit< 可以更轻松地自动调整列大小/a> 方法。
例如,您可以按如下方式调用 AutoFit 方法:
然而,Range.AutoFit 方法可能有点过于激进,导致列太窄。因此,您可能想要创建一个建立最小列宽的方法:
可以按如下方式调用上面的方法,以自动调整列,但最小宽度为 8.5:
您还可以自动调整范围的行。很少需要这样做,但要做到这一点,您可以使用以下内容:
希望这有帮助!
更新:回复克雷格的评论:
嗯,显然您想做两件事:(1) 识别哪些单元格被切断,然后 (2) 纠正这些切断。
我确实理解您希望分两个不同的阶段执行此操作,但步骤 (1) 几乎不可能在所有情况下使用 Excel 正确执行,因为不仅行高和列宽会变化,而且文本换行也可以打开或关闭,并且字体可以是任何数量的潜在样式和/或大小的非比例字体。
简而言之,如果没有大量的工作,就无法可靠地识别哪些细胞被切断。但是,如果您将电子表格限制为仅一种字体样式并使用非比例字体,则可以缓解这种情况。如果这样做,那么您可以简单地将列宽与单元格内文本的长度进行比较。这是因为 Excel.Range.ColumnWidth属性返回其经过校准的宽度,以便一单位的列宽等于普通样式中一个字符的宽度。对于比例字体,使用字符 0(零)的宽度。
因此,对于简单情况,不使用自动换行并且字体为普通样式,并且理想情况下,普通字体是非比例字体,那么您可以循环遍历查找保存值的范围比列宽长:
但现在到了下一部分...您将如何纠正这个问题?同样,如果您有一个非常简单的情况,即不使用自动换行并且字体为普通样式,并且理想情况下,普通字体是非比例字体,那么您有几个选择:
(1)设置列宽以匹配单元格的值长度:
(2) 自动调整列:
但是如果我们要自动调整列,那么直接调用它会更容易,而无需先检查列宽,因为不仅会同时校正所有单元格的宽度,还会校正范围。 AutoFit方法可以处理任何样式的任何字体,包括非比例字体。
因此,直接采用自动调整方法,我们有两个选择:自动调整列或自动调整行。根据您最近的报价,听起来您想调整行的大小:
为此,我将采用文本换行,然后自动调整行。例如,
我认为这些是关于你所有的选择。最后,您想要做的事情和 Excel 能够做的事情可能并不完全匹配,但我认为您应该能够完成您需要做的事情?让我们知道它是否有效......--
迈克
Identifying which column is too wide would be tricky, especially since the text within the cell can have different fonts and/or font sizes. It is much easier to automatically size the columns by making use of the Range.AutoFit method.
As an example, you can call the AutoFit method as follows:
The Range.AutoFit method can be a bit too aggressive, however, resulting in columns that are too narrow. Therefore, you might want to create a method that establishes a minimum column width:
The above could be called as follows, to autofit the columns, but with a minimum width of 8.5:
You can also autofit the Rows of the Range. This is needed less often, but to do so you'd use something like:
Hope this helps!
Update: Reply to Craig's Comment:
Well, clearly you want to do two things: (1) identify which cells are being cut off, and then (2) correct these cutoffs.
I do understand your desire to do this in two distinct stages, but step (1) is almost impossible to do correctly across all circumstances with Excel because not only can row heights and column widths vary, but text wrapping can be on or off, and the font can be a non-proportional font in any number of potential styles and/or sizes.
In short, there will be no way to reliably identify which cells are being cut off, not without an incredible amount of work. It could be mitigated, however, if you restrict the spreadsheet to only one font style and use a non-proportional font. If you do this, then you could simply compare the column width to the length of the text within the cell. This is because the Excel.Range.ColumnWidth property returns its width calibrated so that 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.
Therefore, for simple cases, where word wrap is not employed and the font is in the Normal style, and, ideally, the Normal font is a non-proportional font, then you could loop through all cells in the range looking for where the value held is longer than the column width:
But now here comes the next part... How will you correct this? If, again, you have a very simple situation where word wrap is not employed and the font is in the Normal style, and, ideally, the Normal font is a non-proportional font, then you have a few options:
(1) Set the column width to match the cell's value length:
(2) Auto-fit the Column:
But if we are going to auto-fit the column, then it is much easier to just call it directly, without checking the column widths first, because not only will the widths be corrected for all cells at one time, but the Range.AutoFit method can handle any font, including non-proportional fonts, in any style.
Therefore, going directly to the auto-fit approach, we have two options: either autofit the columns, or autofit the rows. Based on your most recent quote, it sounds like you want to re-size the rows:
For this I would employ text-wrapping and then autofit the rows. For example,
I think that these are about all your options. In the end, what you want to do and what Excel is capable of doing might not match exactly, but I think you should be able to get done what you need to? Let us know if it does the trick...
-- Mike