使用VBA将字符串分隔到excel中的不同单元格中

时间:2012-04-01 09:24:26

标签: excel vba excel-vba

例如,我有一个名为str的字符串变量。该str的值为:

apple
orange
pineapple

每个单词由newVbLine分隔。我想把它移到细胞上。 A1包含apple,A2包含orange,A3包含pineapple

感谢。

1 个答案:

答案 0 :(得分:5)

下面的代码将A列中由vbNewLine分隔的字符串拆分为B列。

请更改
ws1.[b1].Resize(UBound(X) - LBound(X) + 1, 1) = Application.Transpose(X)

ws1.[a1].Resize(UBound(X) - LBound(X) + 1, 1) = Application.Transpose(X)

如果要覆盖A列

Sub Spliced()
    Dim ws1 As Worksheet
    Dim X
    Set ws1 = Sheets(1)
    X = Split(Join(Application.Transpose(ws1.Range(ws1.[a1], ws1.Cells(Rows.Count, "A").End(xlUp))), vbNewLine), vbNewLine)
    ws1.[b1].Resize(UBound(X) - LBound(X) + 1, 1) = Application.Transpose(X)
End Sub

enter image description here