在单元格内分隔成新行

时间:2015-10-29 10:30:26

标签: excel excel-vba vba

我想修改/格式化单元格中的文本,如: this is formatted text that I don't want

如果文字包含“;”或子弹,以换个新行。

简单地说,我将该单元格复制到noteped(.txt)中,并在一行中复制多个项目符号,或者使用“;”。我想要换个新行。现在,我手动修改它。

谢谢!

编辑:

Sub foo1()

Dim txt As String
Dim i As Integer
Dim FullName As Variant
Dim oldText As String
txt = Cells(2, 5)
FullName = Split(txt, ";")

For i = 0 To UBound(FullName)
   oldText = Cells(2, 1).Value
   Cells(2, 1).Value = oldText & vbCrLf & "•" & FullName(i)

Next
End Sub

我成功了。只有两个问题:1。如何放两个分隔符? 2.为什么当我将单元格值复制到记事本中时,它用“”包裹它?

1 个答案:

答案 0 :(得分:1)

如果你想要它在多个单元格中:

Sub fool()

  Dim txt as String, txt2 as variant

  txt = Replace(Cells(2, 5), ".", ";")
  txt2 = Split(txt, ";")

  Range(Cells(2, 1), Cells(Ubound(txt2) + 2, 1) = _
  Application.WorksheetFunction.Transpose(txt2)

End Sub

如果你想要一个单元格中的所有内容:

Sub fool()

  Dim txt as String

  txt = Replace(Cells(2, 5), ".", vbCrLf)
  txt = Replace(txt, ";", vbCrLf)

  Cells(2, 1) = txt

End Sub

嗯......如果你想将旧文本保留在单元格(2,1)中,那么将最后一行更改为:

        Cells(2, 1) = Cells(2, 1) & vbCrLf & txt

编辑:或干净的方式:

Sub fool()

  Dim txt As String, i As Byte

  txt = Cells(2, 1) & vbCrLf & Cells(2, 5)

  'change all delimeters
  For i = 0 To 2
    txt = Replace(txt, Array(".", ";", "•")(i), vbCrLf)
  Next

  'eliminate doubles
  Do While InStr(txt, vbCrLf) > 0
    txt = Replace(txt, vbCrLf & vbCrLf, vbCrLf)
  Loop

  Cells(2, 1) = txt

End Sub