简体   繁体   English

在Excel中自动调整大小和自动换行

[英]Auto Sizing and Auto Wrappin in Excel

So I am looking for some way to auto wrap text in my excel document, posisbly based on number of characters per line. 因此,我正在寻找一种自动在Excel文档中自动换行的方法,该方法基于每行的字符数。

I would also like for my cells to continue auto sizing, I have used the following VBA code for it: 我还希望单元格继续自动调整大小,因此我使用了以下VBA代码:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
For Each Value In Target.Columns
    Worksheets(Sh.Name).Columns(Value.Column).AutoFit
Next Value
Application.ScreenUpdating = True
End Sub

Any ideas? 有任何想法吗?

Is this what you were looking for? 这是您要找的东西吗?

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
Dim Val As Range
For Each Val In Target.Columns
    Worksheets(Sh.Name).Columns(Val.Column).AutoFit
    If Len(Val) > 150 Then
        Val.WrapText = True
    End If
Next Value
Application.ScreenUpdating = True
End Sub

Note: I suggest not using Value as a variable, since it's a "reserved word" in VBA. 注意:建议不要将Value用作变量,因为它是VBA中的“保留字”。 I also assume Value is a cell, so I defined it as a Range. 我还假定Value是一个单元格,因此我将其定义为Range。 It will check if Value is over 150 characters, and if so, wrap text. 它将检查Value是否超过150个字符,如果是,则换行。

If that doesn't work, here's a general way to check if a cell has over 150 characters, and if so, wrap text: 如果这样不起作用,这是一种检查单元格是否超过150个字符的一般方法,如果是,请换行:

Sub autoFitCell()
Dim cel As Range
Set cel = Range("A1")
If Len(cel) > 150 Then
    cel.WrapText = True
End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM