3

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.

2
  • What data range do you want to manipulate - do you have existing data in A2, A3 etc? Commented Apr 1, 2012 at 9:28
  • if those cells are not empty, do overwriting. Commented Apr 1, 2012 at 9:29

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

how do i accept a string variable that contains the text separated by vbNewLine?
@JaysonTamayo - doesn't this code split as you desire? It does need to a be a vbNewLine?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.