Excel VBA-在CHAR(10)后复制并粘贴左侧的文本

时间:2015-05-19 21:24:26

标签: excel-vba vba excel

如果满足以下条件:

A1 = fsfsdfsdfs

qewqeqewqe

B1 = 右(A1,FIND(CHAR(10),A1&""))

然后B1会显示" qewqeqewqe"。

这只能直接进入excel单元格。在上述条件下,如何编写这行代码?

如下所示?

Worksheets("sheet1").Cells(1,2).value= left(find(CHR(10), Worksheets("sheet1").Cells(1,1),&""))?????

我不会问我是否没有搜索过它。我一直在寻找的是如何在excel文件而不是VBA中执行此操作。任何帮助将不胜感激。谢谢!

Sub test()

Dim txt As String
Dim fullname As String

txt = Worksheets("Sheet1").Cells(1, 1).Value
fullname = Split(txt, Chr(10))

Worksheets("Sheet1").Cells(1, 2).Value = fullname

End Sub

1 个答案:

答案 0 :(得分:1)

这应该这样做

Sub Button1_Click()
    Dim rng As Range
    Dim i As Integer
    Dim Orig As Variant
    Dim txt As String

    Set rng = Range("A1")

    txt = rng.Value

    Orig = Split(txt, Chr(10))

    For i = 0 To UBound(Orig)
        Cells(1, i + 1) = Orig(i)
    Next i

End Sub

看看这个, http://www.xlorate.com/vba-examples.html#Split%20Cells