Can you help me split a cell and use first part as a new row heading?

CastingDirector

New Member
Joined
Jun 10, 2014
Messages
46
Please consider helping me with this...
banghead.gif


Here is what I have on a sheet:
NAME (B)Date/time info (C)Agent (D)Role/Notes (E)
Sam Smith4-29-2015 5:15 PMWMJimmy/Has Issues
Bud Light4-29 2015 3:12 PMCAAJimmy/No Travel
**** Wood4-23-2015 4:10 PMFrontXavier/Not interested
Al Kida4-4-2015 2:00 PMManagerPaul/Wife pregnant

<tbody>
</tbody>


So here is what I need in VBA sheet module... Please note: Column E (Role/Notes) should split, then first part is moved to make new row heading in bold and in alphabetical order.
The second part of string remains in the cell. Additional data is frequently copied from another sheet into this as well so it would need to accommodate new data over time.
NAME (B)Date/Time Info(C)Agent(D)Role/Notes(E)
Jimmy
Bud Light4-29-2015 4:10 PMCAANo Travel
Sam Smith4-29-2015 5:15 PMWMHas Issues
Paul
Al Kida4-4-2015 2:00 PMManagerWife Pregnant
Xavier
**** Wood4-23-15 4:10PMFrontNot Interested

<tbody>
</tbody>

I am in the final leg of this 2 year journey to make this right (no kidding). Your help means everything to me. Is this do-able? Does it make sense?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Tested with the data you are showing but try this:

This is based onthe data already being sorted by column E

Code:
Dim RoleT, RoleF As String
Dim LocT, LocF As Integer
Dim lnglstrw As Long

lnglstrw = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

lngRwCnt = 2
RoleT = ""
RoleF = ""
Do While lngRwCnt < lnglstrw + 1

LocT = InStr(Range("E" & lngRwCnt).Value, "/")
RoleT = Left(Range("E" & lngRwCnt).Value, LocT - 1)
Range("e" & lngRwCnt).Value = Right(Range("E" & lngRwCnt).Value, Len(Range("E" & lngRwCnt).Value) - LocT)
If RoleT <> RoleF Then
RoleF = RoleT
Rows(lngRwCnt).Insert
Range("A" & lngRwCnt).Value = RoleF
lngRwCnt = lngRwCnt + 1
lnglstrw = lnglstrw + 1
End If
lngRwCnt = lngRwCnt + 1
Loop
 
Upvote 0
give this a shot.
Code:
Sub reorg()
Dim sh As Worksheet, lr As Long, spl As Variant
Set sh = Sheets(1) 'Edit sheet name
lr = sh.Cells(Rows.Count, "E").End(xlUp).Row
    For i = lr To 2 Step -1
        spl = Split(sh.Cells(i, 5).Value, "/")
        sh.Rows(i).Insert
        sh.Range("A" & i) = Trim(spl(LBound(spl)))
        sh.Range("E" & i + 1) = Trim(spl(UBound(spl)))
    Next
End Sub
 
Upvote 0
This will work much better than my first post.
Code:
Sub reorg()
Dim sh As Worksheet, lr As Long, spl As Variant, fn As Range, i As Long
Set sh = Sheets(1) 'Edit sheet name
lr = sh.Cells(Rows.Count, "E").End(xlUp).Row
    For i = lr To 2 Step -1
        spl = Split(sh.Cells(i, 5).Value, "/")
        If Application.CountIf(sh.Range("B:B"), Trim(spl(LBound(spl)))) > 0 Then
            Set fn = sh.Range("B:B").Find(Trim(spl(LBound(spl))), , xlValues, xlWhole)
                If Not fn Is Nothing Then
                    Rows(i).Copy
                    fn.Offset(1, 0).EntireRow.Insert
                    fn.Offset(1, 3) = Trim(spl(UBound(spl)))
                    Rows(i).Delete
                End If
        Else
            sh.Rows(i).Insert
                sh.Range("B" & i) = Trim(spl(LBound(spl)))
                sh.Range("E" & i + 1) = Trim(spl(UBound(spl)))
        End If
    Next
End Sub
 
Upvote 0
Tested with the data you are showing but try this:

This is based onthe data already being sorted by column E

Code:
Dim RoleT, RoleF As String
Dim LocT, LocF As Integer
Dim lnglstrw As Long

lnglstrw = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

lngRwCnt = 2
RoleT = ""
RoleF = ""
Do While lngRwCnt < lnglstrw + 1

LocT = InStr(Range("E" & lngRwCnt).Value, "/")
RoleT = Left(Range("E" & lngRwCnt).Value, LocT - 1)
Range("e" & lngRwCnt).Value = Right(Range("E" & lngRwCnt).Value, Len(Range("E" & lngRwCnt).Value) - LocT)
If RoleT <> RoleF Then
RoleF = RoleT
Rows(lngRwCnt).Insert
Range("A" & lngRwCnt).Value = RoleF
lngRwCnt = lngRwCnt + 1
lnglstrw = lnglstrw + 1
End If
lngRwCnt = lngRwCnt + 1
Loop

Thanks so much for this. I am getting a "Object Variable or With Block variable not set" I am not sure how to correct...Thoughts?
 
Upvote 0
This will work much better than my first post.
Code:
Sub reorg()
Dim sh As Worksheet, lr As Long, spl As Variant, fn As Range, i As Long
Set sh = Sheets(1) 'Edit sheet name
lr = sh.Cells(Rows.Count, "E").End(xlUp).Row
    For i = lr To 2 Step -1
        spl = Split(sh.Cells(i, 5).Value, "/")
        If Application.CountIf(sh.Range("B:B"), Trim(spl(LBound(spl)))) > 0 Then
            Set fn = sh.Range("B:B").Find(Trim(spl(LBound(spl))), , xlValues, xlWhole)
                If Not fn Is Nothing Then
                    Rows(i).Copy
                    fn.Offset(1, 0).EntireRow.Insert
                    fn.Offset(1, 3) = Trim(spl(UBound(spl)))
                    Rows(i).Delete
                End If
        Else
            sh.Rows(i).Insert
                sh.Range("B" & i) = Trim(spl(LBound(spl)))
                sh.Range("E" & i + 1) = Trim(spl(UBound(spl)))
        End If
    Next
End Sub

Hi and thanks so much for this. I am getting "Sub Script Out of Range" message. "For i = lr To 2 Step -1" needed to change to "For i = lr To 7 Step -1" but still not working. Thoughts?
 
Upvote 0
Hi,

It might be I did not define the row count variable (lngrwcnt) it worked OK in mine but that might just be a version thing. Try this one:

Code:
Dim RoleT, RoleF As String
Dim LocT, LocF As Integer
Dim lnglstrw, lngrwcnt As Long

lnglstrw = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

lngRwCnt = 2
RoleT = ""
RoleF = ""
Do While lngRwCnt < lnglstrw + 1

LocT = InStr(Range("E" & lngRwCnt).Value, "/")
RoleT = Left(Range("E" & lngRwCnt).Value, LocT - 1)
Range("e" & lngRwCnt).Value = Right(Range("E" & lngRwCnt).Value, Len(Range("E" & lngRwCnt).Value) - LocT)
If RoleT <> RoleF Then
RoleF = RoleT
Rows(lngRwCnt).Insert
Range("A" & lngRwCnt).Value = RoleF
lngRwCnt = lngRwCnt + 1
lnglstrw = lnglstrw + 1
End If
lngRwCnt = lngRwCnt + 1
Loop
 
Upvote 0
Hi and thanks so much for this. I am getting "Sub Script Out of Range" message. "For i = lr To 2 Step -1" needed to change to "For i = lr To 7 Step -1" but still not working. Thoughts?

That is odd for that error message on that particular line of code, since there is no object for it to seek on that line. If you edited the code before running, be sure sheet references and spelling are correct. Also, some error messages are generated when cell A1 is blank and that can be fixed by simply putting an asterisk in the cell. The code ran without error for me while using the sample in the OP.
 
Upvote 0
I have tried several ways to duplicate the "Subscript out of range" error and cannot do it. It still remains a mystery to me of why that line was highlighted.
 
Upvote 0
The only way I could get the error message was to put the code into a sheet code module and run the code twice without resetting the rows. The code should be in the standard module 1, not the sheet code module.
 
Upvote 0

Forum statistics

Threads
1,216,174
Messages
6,129,297
Members
449,499
Latest member
HockeyBoi

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