查找太小而无法显示内容的文本单元格

发布于 2024-08-18 18:10:05 字数 100 浏览 3 评论 0原文

我有一个工作表,其中的单元格可能包含有时超出单元格宽度和高度的文本。如果我没有注意到它并调整行高,文本将显示为截止。有谁有可以定位这些单元格以便我调整它们的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑依然 2024-08-25 18:10:05

确定哪一列太宽会很棘手,特别是因为单元格内的文本可以具有不同的字体和/或字体大小。使用 Range.AutoFit< 可以更轻松地自动调整列大小/a> 方法。

例如,您可以按如下方式调用 AutoFit 方法:

Dim myRange As Excel.Range
Set myRange = Application.Range("A1:C3")
myRange.Columns.AutoFit

然而,Range.AutoFit 方法可能有点过于激进,导致列太窄。因此,您可能想要创建一个建立最小列宽的方法:

Sub AutoFitColumns(theRange As Excel.Range, minColumnWidth As Double)
    theRange.Columns.AutoFit

    Dim column As Excel.Range

    For Each column In theRange.Columns
        If column.ColumnWidth < minColumnWidth Then
            column.ColumnWidth = minColumnWidth
        End If
    Next column
End Sub

可以按如下方式调用上面的方法,以自动调整列,但最小宽度为 8.5:

Dim myRange As Excel.Range
Set myRange = Application.Range("A1:C3")
Call AutoFitColumns(myRange, 8.5)

您还可以自动调整范围的行。很少需要这样做,但要做到这一点,您可以使用以下内容:

myRange.Rows.AutoFit

希望这有帮助!

更新:回复克雷格的评论:

谢谢迈克。该电子表格用于
作为网络界面的结构化输入
生成器,因此列宽有所不同
并且不应该调整。我真的是
只是想找一些东西来扫描
包含文本的任何单元格的工作表
显示的宽度比单元格宽
将允许适合,因此需要
行高尺寸较大。我不是
寻找一个过程来使
调整,只要找到它们,因为它们
很容易被忽视。任何想法
那?

嗯,显然您想做件事:(1) 识别哪些单元格被切断,然后 (2) 纠正这些切断。

我确实理解您希望分两个不同的阶段执行此操作,但步骤 (1) 几乎不可能在所有情况下使用 Excel 正确执行,因为不仅行高和列宽会变化,而且文本换行也可以打开或关闭,并且字体可以是任何数量的潜在样式和/或大小的非比例字体。

简而言之,如果没有大量的工作,就无法可靠地识别哪些细胞被切断。但是,如果您将电子表格限制为仅一种字体样式并使用非比例字体,则可以缓解这种情况。如果这样做,那么您可以简单地将列宽与单元格内文本的长度进行比较。这是因为 Excel.Range.ColumnWidth属性返回其经过校准的宽度,以便一单位的列宽等于普通样式中一个字符的宽度。对于比例字体,使用字符 0(零)的宽度。

因此,对于简单情况,不使用自动换行并且字体为普通样式,并且理想情况下,普通字体是非比例字体,那么您可以循环遍历查找保存值的范围比列宽长:

Dim myRange As Range
Set myRange = Application.Range("A1:E5")

Dim cell As Excel.Range
For Each cell in myRange.Cells
    If Len(CStr(cell.Value)) > cell.ColumnWidth Then
        MsgBox "The cell at " & cell.Address & " has text that is too long!"
    End if
Next cell

但现在到了下一部分...您将如何纠正这个问题?同样,如果您有一个非常简单的情况,即不使用自动换行并且字体为普通样式,并且理想情况下,普通字体是非比例字体,那么您有几个选择:

(1)设置列宽以匹配单元格的值长度:

Dim myRange As Range
Set myRange = Application.Range("A1:E5")

Dim cell As Excel.Range
For Each cell in myRange.Cells
    If Len(CStr(cell.Value)) > cell.ColumnWidth Then
        cell.ColumnWidth = Len(CStr(cell.Value))
    End if
Next cell

(2) 自动调整列:

Dim myRange As Range
Set myRange = Application.Range("A1:E5")

Dim cell As Excel.Range
For Each cell in myRange.Cells
    If Len(CStr(cell.Value)) > cell.ColumnWidth Then
        cell.Columns.AutoFit
    End if
Next cell

但是如果我们要自动调整列,那么直接调用它会更容易,而无需先检查列宽,因为不仅会同时校正所有单元格的宽度,还会校正范围。 AutoFit方法可以处理任何样式的任何字体,包括非比例字体。

因此,直接采用自动调整方法,我们有两个选择:自动调整列或自动调整行。根据您最近的报价,听起来您想调整的大小:

我真的
只是想找一些东西来扫描
包含文本的任何单元格的工作表
显示的宽度比单元格宽
将允许适合,因此需要
行高尺寸变大。

为此,我将采用文本换行,然后自动调整行。例如,

Dim rng As Excel.Range
Set rng = Application.Range("A1:E5")

With rng.Rows
    .WrapText = True
    .AutoFit
End With

我认为这些是关于你所有的选择。最后,您想要做的事情和 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:

Dim myRange As Excel.Range
Set myRange = Application.Range("A1:C3")
myRange.Columns.AutoFit

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:

Sub AutoFitColumns(theRange As Excel.Range, minColumnWidth As Double)
    theRange.Columns.AutoFit

    Dim column As Excel.Range

    For Each column In theRange.Columns
        If column.ColumnWidth < minColumnWidth Then
            column.ColumnWidth = minColumnWidth
        End If
    Next column
End Sub

The above could be called as follows, to autofit the columns, but with a minimum width of 8.5:

Dim myRange As Excel.Range
Set myRange = Application.Range("A1:C3")
Call AutoFitColumns(myRange, 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:

myRange.Rows.AutoFit

Hope this helps!

Update: Reply to Craig's Comment:

Thansk Mike. This spreadsheet is used
as sturctured input to a web interface
generator so the column widths vary
and shouldn't be adjusted. I'm really
just looking for something to scan a
sheet for any cells where the text
displayed is wider than what the cell
will allow to fit, thereby needing the
row height sized larger. I'm not
looking for a process to make the
adjustment, just find them since they
are easy to overlook. Any ideas on
that?

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:

Dim myRange As Range
Set myRange = Application.Range("A1:E5")

Dim cell As Excel.Range
For Each cell in myRange.Cells
    If Len(CStr(cell.Value)) > cell.ColumnWidth Then
        MsgBox "The cell at " & cell.Address & " has text that is too long!"
    End if
Next cell

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:

Dim myRange As Range
Set myRange = Application.Range("A1:E5")

Dim cell As Excel.Range
For Each cell in myRange.Cells
    If Len(CStr(cell.Value)) > cell.ColumnWidth Then
        cell.ColumnWidth = Len(CStr(cell.Value))
    End if
Next cell

(2) Auto-fit the Column:

Dim myRange As Range
Set myRange = Application.Range("A1:E5")

Dim cell As Excel.Range
For Each cell in myRange.Cells
    If Len(CStr(cell.Value)) > cell.ColumnWidth Then
        cell.Columns.AutoFit
    End if
Next cell

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:

I'm really
just looking for something to scan a
sheet for any cells where the text
displayed is wider than what the cell
will allow to fit, thereby needing the
row height sized larger.

For this I would employ text-wrapping and then autofit the rows. For example,

Dim rng As Excel.Range
Set rng = Application.Range("A1:E5")

With rng.Rows
    .WrapText = True
    .AutoFit
End With

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文