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:

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Column O[/TD]
[TD]Column P[/TD]
[/TR]
[TR]
[TD]Michael Smith
Matt Kennedy[/TD]
[TD]Vice President
President[/TD]
[/TR]
</tbody>[/TABLE]


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:

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Column O[/TD]
[TD]Column P[/TD]
[/TR]
[TR]
[TD]Michael Smith[/TD]
[TD]Vice President[/TD]
[/TR]
[TR]
[TD]Matt Kennedy[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

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:

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Column O[/TD]
[TD]Column P[/TD]
[/TR]
[TR]
[TD]Michael Smith
Matt Kennedy (shouldn't appear here)[/TD]
[TD]Vice President[/TD]
[/TR]
[TR]
[TD]Michael Smith (shouldn't appear here)
Matt Kennedy[/TD]
[TD]President[/TD]
[/TR]
</tbody>[/TABLE]

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:
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
Michael 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>
[TD="bgcolor: #cacaca, align: center"]1[/TD]

</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

After:

*AB
Michael SmithVice President
Matt KennedyPresident

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:152.67px;"><col style="width:114.67px;"></colgroup><tbody>
[TD="bgcolor: #cacaca, align: center"]1[/TD]

[TD="bgcolor: #cacaca, align: center"]2[/TD]

</tbody>


Excel tables to the web >> Excel Jeanie HTML 4
 
Last edited:
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