Moving text

handysmurf

Board Regular
Joined
Jan 17, 2015
Messages
120
Office Version
  1. 365
Platform
  1. Windows
I need to move text from one cell to another ... without overwriting the data already in the cell. Also entering a line return between the new text and old. I have many many cells that I need to do this with.

1738938488388.png


So in this example copy from C2 to D2... it would end up looking like D1
 
Suggest this change

With Range("C1:D" & Range("A" & Rows.Count).End(3).Row)
I missed putting that part. Thanks Peter.


Here is my macro updated with Peter's observation.

VBA Code:
Sub MovingText()
  Dim i As Long, a As Variant
  
  With Range("C1:D" & Range("A" & Rows.Count).End(3).Row)
    a = .Value
    For i = 1 To UBound(a, 1)
      If a(i, 1) <> "" Then
        a(i, 2) = a(i, 1) & Chr(10) & a(i, 2)
        a(i, 1) = ""
      End If
    Next
    .Value = a
  End With
End Sub
 
Upvote 0

I don't understand how the macro works for me with the same example, but with your example the macro sends an error. Maybe it's the excel version.
My mistake was knowing which column contained the last row with data and I forgot to set the Row property.

The important thing is that you already have a solution.
:love:
 
Upvote 0
BTW, Dante's code (& mine) allow for if a cell in column C is empty but not if a cell in column D is empty (or both). In case that can happen (& you don't want a stray linefeed in column D data) ..

VBA Code:
Sub MovingText_v2()
  Dim i As Long, a As Variant
 
  With Range("C1:D" & Range("A" & Rows.Count).End(3).Row)
    a = .Value
    For i = 1 To UBound(a, 1)
      If a(i, 1) <> "" Then
        a(i, 2) = a(i, 1) & IIf(a(i, 2) = "", "", Chr(10) & a(i, 2))
        a(i, 1) = ""
      End If
    Next
    .Value = a
  End With
End Sub

.. or you could do it all at once without any looping

VBA Code:
Sub MovingText_v3()
  With Range("C1:D" & Range("A" & Rows.Count).End(xlUp).Row)
    .Columns(2).Value = Evaluate("BYROW(" & .Address & ",LAMBDA(r,TEXTJOIN(CHAR(10),1,r)))")
    .Columns(1).ClearContents
  End With
End Sub
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top