VBA Text to Row with Row Insert ???

Snaps

New Member
Joined
Nov 11, 2010
Messages
29
Lately I've been running reports from our system that require partnerships that export in a single row to be split into their own separate rows. The names are always separated by a "/".

What I'm hoping to have happen is for a row to be inserted for every "/" that is in the A|B Columns. And then have the information in those cells split.

Example Before:
Code:
<TABLE style="WIDTH: 213pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=284 x:str><COLGROUP><COL style="WIDTH: 53pt; mso-width-source: userset; mso-width-alt: 2596" width=71><COL style="WIDTH: 80pt; mso-width-source: userset; mso-width-alt: 3876" width=106><COL style="WIDTH: 80pt; mso-width-source: userset; mso-width-alt: 3913" width=107><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 53pt; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl24 height=17 width=71>[B][FONT=Arial][SIZE=2]First Name[/SIZE][/FONT][/B]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 80pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl24 width=106>[B][FONT=Arial][SIZE=2]Last Name[/SIZE][/FONT][/B]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 80pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl24 width=107>[B][FONT=Arial][SIZE=2]Address[/SIZE][/FONT][/B]</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>[FONT=Arial][SIZE=2]Jim/Tom[/SIZE][/FONT]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">[FONT=Arial][SIZE=2]Stevenson/Smith[/SIZE][/FONT]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">[FONT=Arial][SIZE=2]1432 Street Drive[/SIZE][/FONT]</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>[FONT=Arial][SIZE=2]Chris[/SIZE][/FONT]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">[FONT=Arial][SIZE=2]Adams[/SIZE][/FONT]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">[FONT=Arial][SIZE=2]4813 Road[/SIZE][/FONT]</TD></TR></TBODY></TABLE>

Example After:
Code:
<TABLE style="WIDTH: 213pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=284 x:str><COLGROUP><COL style="WIDTH: 53pt; mso-width-source: userset; mso-width-alt: 2596" width=71><COL style="WIDTH: 80pt; mso-width-source: userset; mso-width-alt: 3876" width=106><COL style="WIDTH: 80pt; mso-width-source: userset; mso-width-alt: 3913" width=107><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 53pt; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl24 height=17 width=71>[B][FONT=Arial][SIZE=2]First Name[/SIZE][/FONT][/B]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 80pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl24 width=106>[B][FONT=Arial][SIZE=2]Last Name[/SIZE][/FONT][/B]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 80pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl24 width=107>[B][FONT=Arial][SIZE=2]Address[/SIZE][/FONT][/B]</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>[FONT=Arial][SIZE=2]Jim[/SIZE][/FONT]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">[FONT=Arial][SIZE=2]Stevenson[/SIZE][/FONT]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">[FONT=Arial][SIZE=2]1432 Street Drive[/SIZE][/FONT]</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>[FONT=Arial][SIZE=2]Tom[/SIZE][/FONT]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">[FONT=Arial][SIZE=2]Smith[/SIZE][/FONT]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">[FONT=Arial][SIZE=2]1433 Street Drive[/SIZE][/FONT]</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>[FONT=Arial][SIZE=2]Chris[/SIZE][/FONT]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">[FONT=Arial][SIZE=2]Adams[/SIZE][/FONT]</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">[FONT=Arial][SIZE=2]4813 Road[/SIZE][/FONT]</TD></TR></TBODY></TABLE>

Any help with this would be huge. My VBA experience is very limited but I'd love to figure it out.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Place this in a standard Module.
Change sheet names to match yours.
Code:
Sub NameSplit()
  Dim DataTable As Range, r As Long, s As Long, L As Long
  'set DataTable range without header
  Set DataTable = Sheets("Before").Range("A1").CurrentRegion
  Set DataTable = DataTable.Offset(1, 0).Resize(DataTable.Rows.Count - 1, DataTable.Columns.Count)
  'get results sheet ready
  Sheets("After").Cells.ClearContents
  Sheets("Before").Rows(1).Copy Sheets("After").Rows(1)
  L = 2
  For r = 1 To DataTable.Rows.Count
    'check if number of splits on first & last names are equal
    If Len(DataTable.Cells(r, 1)) - Len(WorksheetFunction.Substitute(DataTable.Cells(r, 1), "/", "")) _
      = Len(DataTable.Cells(r, 2)) - Len(WorksheetFunction.Substitute(DataTable.Cells(r, 2), "/", "")) Then
      'splits are equal - split data
      For s = 0 To Len(DataTable.Cells(r, 1)) - Len(WorksheetFunction.Substitute(DataTable.Cells(r, 1), "/", ""))
        Sheets("After").Cells(L, 1) = Split(DataTable.Cells(r, 1), "/")(s)
        Sheets("After").Cells(L, 2) = Split(DataTable.Cells(r, 2), "/")(s)
        Sheets("After").Cells(L, 3) = DataTable.Cells(r, 3)
        L = L + 1
      Next s
    Else
      'splits are NOT equal - leave data as is
      Sheets("After").Cells(L, 1) = DataTable.Cells(r, 1)
      Sheets("After").Cells(L, 2) = DataTable.Cells(r, 2)
      Sheets("After").Cells(L, 3) = DataTable.Cells(r, 3)
      L = L + 1
    End If
  Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,733
Members
452,939
Latest member
WCrawford

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