Copying/Pasting a line down with VBA

mikec82

Board Regular
Joined
Jan 13, 2009
Messages
225
In a sheet called "$2500-$4999" I have a range A1:P35 (16 rows). In columns O and P, there are two lines of text separated by a line break like this:

Column OColumn P
Michael Smith
Matt Kennedy
Vice President
President

<tbody>
</tbody>


I'm trying to come up with some VBA that will copy the entire row (A2:P2, for instance), insert a new row below, paste the text, and split the line breaks so one is now in each cell, like this:

Column OColumn P
Michael SmithVice President
Matt Kennedy

<tbody>
</tbody>

I'm working with the code at the bottom of this post, and am very close, but for some reason, everything works correctly, only Column O is not splitting correctly and looks like this:

Column OColumn P
Michael Smith
Matt Kennedy (shouldn't appear here)
Vice President
Michael Smith (shouldn't appear here)
Matt Kennedy
President

<tbody>
</tbody>

Would I need to do two seperate VBA macros? I am not sure what is wrong with the code - any ideas?


Sub ExpandIfBreak()
Sheets("$2500-$4999").Select
Dim lR As Long, R As Range, i As Long, n As Long, j As Long, ct As Long, V As Variant
lR = Range("A" & Rows.Count).End(xlUp).Row
Set R = Range("A1:P35" & lR)
Application.ScreenUpdating = False
For i = R.Rows.Count To 1 Step -1
n = InStr(R.Rows(i).Cells(1, 7), Chr(10))
If n > 0 Then
ct = 0
For j = 1 To Len(R.Rows(i).Cells(1, 7))
If Mid(R.Rows(i).Cells(1, 7).Value, j, 1) = Chr(10) Then
ct = ct + 1
End If
Next j
j = 0
R.Rows(i).Offset(1, 0).Resize(ct).EntireRow.Insert
R.Rows(i).Resize(ct + 1).FillDown
V = Split(R.Rows(i).Cells(1, 7).Value, Chr(10))
For j = 0 To ct
R.Rows(i + j).Cells(1, 7).Value = V(j)
Next j
j = 0
End If
Next i
Sheets("Data").Select
End Sub
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
It's difficult to help you without an actual file to use for testing.

Here's an example, maybe it will help you.

You can see the before and after below the code.
Code:
Sub SplitTest()
Dim a, j
    For Each j In Range("A1:B1")
        a = Split(j, Chr(10))
        j.Insert shift:=xlDown
        j.Offset(-1).Value = a(0): j.Value = a(1)
    Next j
End Sub

Before:

*AB
1Michael Smith
Matt Kennedy
Vice President
President

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:152.67px;"><col style="width:114.67px;"></colgroup><tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

After:

*AB
1Michael SmithVice President
2Matt KennedyPresident

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:152.67px;"><col style="width:114.67px;"></colgroup><tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,840
Members
449,471
Latest member
lachbee

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