Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate strings into different cells in excel using VBA

Tags:

excel

vba

For example, I have a string variable named str. This str has a value of:

apple
orange
pineapple

Each word is separated by a newVbLine. I want to move it on cells. A1 contains apple, A2 contains orange and A3 contains pineapple.

Thanks.

like image 304
Jayson Tamayo Avatar asked Dec 29 '25 01:12

Jayson Tamayo


1 Answers

The code below splits strings delimited by vbNewLine in column A into column B.

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

if you want to overwrite column 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

like image 66
brettdj Avatar answered Dec 31 '25 18:12

brettdj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!