0

The output of a script I have is generating a string of two sentences, now separated only with a space. Currently, the script has joined the strings from A1 and A2 to get A3

A1 Mary had

A2 a little lamb

A3 Mary had a little lamb

However, I'd like to insert a carriage return to separate the two strings using the replace function.

It feels like there should be a way to have excel count the string in A1 and then add a carriage return that far into A3.

What I have so far is below, but it's currently replacing all of Mary had with a return. Is there something simple I'm missing?

Range = Replace(A3, Left(A3, InStr(A3.Value, A1.Value)), vbLf)

Much appreciated.

0

2 Answers 2

1

If I understand correctly, what you want to do is concatenate the values of cells A1 and A2, adding a new line between them, and set the result to cell A3. You can do it fairly easy as such :

A3.Value = A1.Value & vbNewLine & A2.Value

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

Comments

1

This is based off the guess that A1 and A2 are already designated as strings. I dont think you need a replace function unless its required for something else in your project.

Option Explicit
Sub Test()
    Dim A1 As String, A2 As String, A3 As String

    A1 = "Mary had"
    A2 = "a little lamb"

    A3 = A1 & vbCrLf & A2

    Debug.Print A3

End Sub

Comments

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.