通过代码在包装单元中插入换行符。

时间:2022-03-29 03:00:30

Is it possible to insert line break in a wrapped cell through VBA code? (similar to doing Alt-Enter when entering data manually)

是否可以通过VBA代码在包装的单元格中插入换行符?(类似手工输入数据时做Alt-Enter)

I have set the cell's wrap text property to True via VBA code, and I am inserting data into it also through VBA code.

我已经通过VBA代码将单元格的wrap文本属性设置为True,我也通过VBA代码将数据插入到其中。

4 个解决方案

#1


50  

Yes. The VBA equivalent of AltEnter is to use a linebreak character:

是的。VBA等效的AltEnter是使用linebreak字符:

ActiveCell.Value = "I am a " & Chr(10) & "test"

Note that this automatically sets WrapText to True.

注意,这将自动将WrapText设置为True。

Proof:

证明:

Sub test()
Dim c As Range
Set c = ActiveCell
c.WrapText = False
MsgBox "Activcell WrapText is " & c.WrapText
c.Value = "I am a " & Chr(10) & "test"
MsgBox "Activcell WrapText is " & c.WrapText
End Sub

#2


10  

You could also use vbCrLf which corresponds to Chr(13) & Chr(10).

你也可以使用vbCrLf,它对应于Chr(13)和Chr(10)。

#3


-1  

Yes there are two way to add a line feed:

有两种方法可以添加换行:

  1. Use the existing function from VBA vbCrLf in the string you want to add a line feed, as such:

    在要添加线提要的字符串中使用VBA vbCrLf的现有函数,如下所示:

    Dim text As String

    昏暗的文本字符串

    text = "Hello" & vbCrLf & "World!"

    文本=“Hello”& vbCrLf &“World!”

    Worksheets(1).Cells(1, 1) = text

    工作表(1)。细胞(1,- 1)=文本

  2. Use the Chr() function and pass the ASCII characters 13 and 10 in order to add a line feed, as shown bellow:

    使用Chr()函数并传递ASCII字符13和10,以便添加行提要,如下所示:

    Dim text As String

    昏暗的文本字符串

    text = "Hello" & Chr(13) & Chr(10) & "World!"

    文本=“Hello”& Chr(13) & Chr(10)和“World!”

    Worksheets(1).Cells(1, 1) = text

    工作表(1)。细胞(1,- 1)=文本

In both cases, you will have the same output in cell (1,1) or A1.

在这两种情况下,单元格(1,1)或A1中的输出都是相同的。

#4


-1  

Just do Ctrl + Enter inside the text box

只需按Ctrl + Enter键进入文本框

#1


50  

Yes. The VBA equivalent of AltEnter is to use a linebreak character:

是的。VBA等效的AltEnter是使用linebreak字符:

ActiveCell.Value = "I am a " & Chr(10) & "test"

Note that this automatically sets WrapText to True.

注意,这将自动将WrapText设置为True。

Proof:

证明:

Sub test()
Dim c As Range
Set c = ActiveCell
c.WrapText = False
MsgBox "Activcell WrapText is " & c.WrapText
c.Value = "I am a " & Chr(10) & "test"
MsgBox "Activcell WrapText is " & c.WrapText
End Sub

#2


10  

You could also use vbCrLf which corresponds to Chr(13) & Chr(10).

你也可以使用vbCrLf,它对应于Chr(13)和Chr(10)。

#3


-1  

Yes there are two way to add a line feed:

有两种方法可以添加换行:

  1. Use the existing function from VBA vbCrLf in the string you want to add a line feed, as such:

    在要添加线提要的字符串中使用VBA vbCrLf的现有函数,如下所示:

    Dim text As String

    昏暗的文本字符串

    text = "Hello" & vbCrLf & "World!"

    文本=“Hello”& vbCrLf &“World!”

    Worksheets(1).Cells(1, 1) = text

    工作表(1)。细胞(1,- 1)=文本

  2. Use the Chr() function and pass the ASCII characters 13 and 10 in order to add a line feed, as shown bellow:

    使用Chr()函数并传递ASCII字符13和10,以便添加行提要,如下所示:

    Dim text As String

    昏暗的文本字符串

    text = "Hello" & Chr(13) & Chr(10) & "World!"

    文本=“Hello”& Chr(13) & Chr(10)和“World!”

    Worksheets(1).Cells(1, 1) = text

    工作表(1)。细胞(1,- 1)=文本

In both cases, you will have the same output in cell (1,1) or A1.

在这两种情况下,单元格(1,1)或A1中的输出都是相同的。

#4


-1  

Just do Ctrl + Enter inside the text box

只需按Ctrl + Enter键进入文本框