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!
问题:
回答1:
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