VB :: Break Down Cell Into Multiple Cells and Categories

Craig.Cochran

New Member
Joined
Oct 30, 2010
Messages
16
I have an old Excel sheet that breaks down my employee's tasks throughout the day. The issue I have is that the tasks are bundled within one cell and I would like to use VB to extract the data as indicated below:

<TABLE style="WIDTH: 53pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=70 border=0 x:str><COLGROUP><COL style="WIDTH: 53pt; mso-width-source: userset; mso-width-alt: 2986" width=70><TBODY><TR style="HEIGHT: 123.75pt" height=165><TD class=xl27 style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; WIDTH: 53pt; BORDER-BOTTOM: #d4d0c8; HEIGHT: 123.75pt; BACKGROUND-COLOR: transparent" width=70 height=165>07:00 - 11:00 (On Duty) 11:00 - 11:30 (Lunch) 11:30 - 11:45 (Conference Call) 11:45 - 15:00 (On Duty)</TD></TR></TBODY></TABLE>
<TABLE style="WIDTH: 272pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=362 border=0 x:str><COLGROUP><COL style="WIDTH: 54pt; mso-width-source: userset; mso-width-alt: 3072" width=72><COL style="WIDTH: 109pt; mso-width-source: userset; mso-width-alt: 6186" span=2 width=145><TBODY><TR style="HEIGHT: 11.25pt" height=15><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 54pt; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 11.25pt; BACKGROUND-COLOR: #3366ff" width=72 height=15>Task 1 - Start</TD><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 109pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: #3366ff" width=145>Task 1 - End</TD><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 109pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: #3366ff" width=145>Task 1 - Category</TD></TR><TR style="HEIGHT: 11.25pt" height=15><TD class=xl28 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 11.25pt; BACKGROUND-COLOR: transparent" height=15 x:num="0.29166666666666669">7:00</TD><TD class=xl28 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" x:num="0.45833333333333331">11:00</TD><TD class=xl27 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent">On Duty</TD></TR><TR style="HEIGHT: 11.25pt" height=15><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 11.25pt; BACKGROUND-COLOR: #3366ff" height=15>Task 2 - Start</TD><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: #3366ff">Task 2 - End</TD><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: #3366ff">Task 2 - Category</TD></TR><TR style="HEIGHT: 11.25pt" height=15><TD class=xl28 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 11.25pt; BACKGROUND-COLOR: transparent" height=15 x:num="0.45833333333333331">11:00</TD><TD class=xl28 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" x:num="0.47916666666666669">11:30</TD><TD class=xl27 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent">Lunch</TD></TR><TR style="HEIGHT: 11.25pt" height=15><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 11.25pt; BACKGROUND-COLOR: #3366ff" height=15>Task 3 - Start</TD><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: #3366ff">Task 3 - End</TD><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: #3366ff">Task 3 - Category</TD></TR><TR style="HEIGHT: 11.25pt" height=15><TD class=xl28 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 11.25pt; BACKGROUND-COLOR: transparent" height=15 x:num="0.47916666666666669">11:30</TD><TD class=xl28 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" x:num="0.48958333333333331">11:45</TD><TD class=xl27 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent">Conference Call</TD></TR><TR style="HEIGHT: 11.25pt" height=15><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 11.25pt; BACKGROUND-COLOR: #3366ff" height=15>Task 4 - Start</TD><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: #3366ff">Task 4 - End</TD><TD class=xl29 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: #3366ff">Task 4 - Category</TD></TR><TR style="HEIGHT: 11.25pt" height=15><TD class=xl28 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 11.25pt; BACKGROUND-COLOR: transparent" height=15 x:num="0.48958333333333331">11:45</TD><TD class=xl28 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" x:num="0.625">15:00</TD><TD class=xl27 style="BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent">On Duty</TD></TR></TBODY></TABLE>


Note: Not all employees have the same number of tasks, nor the same type.

Any assistance with this would be greatly appreciated!

Respectfully,

Craig Cochran
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Is the 'before' data shown all in one cell, like:
<TABLE style="WIDTH: 128pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=170 border=0 x:str><COLGROUP><COL style="WIDTH: 128pt; mso-width-source: userset; mso-width-alt: 6217" width=170><TBODY><TR style="HEIGHT: 51.75pt" height=69><TD class=xl22 style="BORDER-RIGHT: windowtext 1pt solid; BORDER-TOP: windowtext 1pt solid; BORDER-LEFT: windowtext 1pt solid; WIDTH: 128pt; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 51.75pt; BACKGROUND-COLOR: transparent" width=170 height=69>07:00 - 11:00 (On Duty) 11:00 - 11:30 (Lunch) 11:30 - 11:45 (Conference Call) 11:45 - 15:00 (On Duty)</TD></TR></TBODY></TABLE>
 
Upvote 0
Simply as a start, to see if we can get a dependable pattern. For:
Excel Workbook
BCDEF
1BeforeAfter
207:00 - 11:00 (On Duty) 11:00 - 11:30 (Lunch) 11:30 - 11:45 (Conference Call) 11:45 - 15:00 (On Duty)Task 17:00:00 AM11:00:00 AMOn Duty
307:00 - 11:00 (On Duty) 11:00 - 11:30 (Lunch) 11:30 - 11:45 (Conference Call) 11:45 - 15:00 (On Duty)Task 111:00:00 AM11:30:00 AMLunch
407:00 - 11:00 (On Duty) 11:00 - 11:30 (Lunch) 11:30 - 11:45 (Conference Call) 11:45 - 15:00 (On Duty)Task 111:30:00 AM11:45:00 AMConference Call
5Task 111:45:00 AM3:00:00 PMOn Duty
6
7
8
Sheet1
Excel 2003
Maybe:
Rich (BB code):
Option Explicit
    
Sub SplitText()
Dim REX             As Object '<--- RegExp
Dim rexMatchCol     As Object '<--- MatchCollection
Dim rexMatch        As Object '<--- Match
Dim wks             As Worksheet
Dim rngData         As Range
Dim Cell            As Range
Dim vntStart        As Variant
Dim vntEnd          As Variant
Dim strHours        As String
Dim strDuty         As String
Dim lRowOffset      As Long
Dim lTask           As Long
Dim aryTimes        As Variant
    
    
    Set wks = ThisWorkbook.Worksheets("Sheet1") '<---Change name to suit
    Set rngData = Range(wks.Cells(2, 2), wks.Cells(wks.Rows.Count, 2).End(xlUp))
    Set REX = CreateObject("VBScript.RegExp")
    
    With REX
        .Global = True
        .Pattern = "([0-9\ \:\-]+?\()([A-Za-z\ ]+\))"
        
        lRowOffset = 0
        For Each Cell In rngData
        lTask = lTask + 1
            If .Test(Cell.Value) Then
                Set rexMatchCol = .Execute(Cell.Value)
                
                For Each rexMatch In rexMatchCol
                    strHours = Replace(rexMatch.SubMatches(0), "(", vbNullString)
                    aryTimes = Split(strHours, "-")
                    vntStart = Trim(aryTimes(0))
                    vntEnd = Trim(aryTimes(1))
                    strDuty = Replace(rexMatch.SubMatches(1), ")", vbNullString)
                        
                    Cell.Offset(lRowOffset, 1).Value = "Task " & lTask
                    Cell.Offset(lRowOffset, 2).Value = CDate(vntStart)
                    Cell.Offset(lRowOffset, 3).Value = CDate(vntEnd)
                    Cell.Offset(lRowOffset, 4).Value = strDuty
                    lRowOffset = lRowOffset + 1
                Next
                lRowOffset = lRowOffset - 1
            End If
        Next
    End With
End Sub

I would suggest testing (in a junk copy of your wb) to see if the .Pattern jambs on anything possibilities you may have not yet noticed.

Again, just a start, as hopefully others will join and show no doubt better RegExp.

Mark
 
Upvote 0
This does work well for what I am trying to accomplish - I am sure that I will be able to use simple VB to extract and place the data from here. The only concern I have is when I input more than five timed "duties", it is not populating down. At times, these employees will be scheduled for a lot of conference calls throughout a shift.

Thank you much for the help thus far!
 
Upvote 0
Not quite sure how the original data is laid out or where the results should go but see if this is any help. It assumes original data is in column A starting in A2 and lays out results across the sheet as shown below.

Code:

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Tasks()<br>    <SPAN style="color:#00007F">Dim</SPAN> cel <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, c <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, t <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> Bits, Headings<br>    <br>    Headings = Array("Task # - Start", "Task # - End", "Task # - Category")<br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cel <SPAN style="color:#00007F">In</SPAN> Range("A2", Range("A" & Rows.Count).End(xlUp))<br>        c = c + 4: r = 1: t = 0<br>        s = cel.Value<br>        s = Replace(Replace(s, "(", ")"), " - ", ")")<br>        Bits = Split(s, ")")<br>        <SPAN style="color:#00007F">For</SPAN> i = 0 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(Bits) - 1 <SPAN style="color:#00007F">Step</SPAN> 3<br>            t = t + 1<br>            <SPAN style="color:#00007F">With</SPAN> Cells(r, c).Resize(, 3)<br>                .Value = Headings<br>                .Replace What:="#", Replacement:=t, Lookat:=xlPart, _<br>                    SearchFormat:=False, Replace<SPAN style="color:#00007F">For</SPAN>mat:=False<br>                .Interior.ColorIndex = 41<br>                .Font.ColorIndex = 2<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            For j = 0 <SPAN style="color:#00007F">To</SPAN> 2<br>                Cells(r + 1, c + j).Value = Bits(i + j)<br>            <SPAN style="color:#00007F">Next</SPAN> j<br>            r = r + 2<br>        <SPAN style="color:#00007F">Next</SPAN> i<br>        <SPAN style="color:#00007F">With</SPAN> Columns(c).Resize(, 3)<br>            .AutoFit<br>            .HorizontalAlignment = xlLeft<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> cel<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>


My sample data and results:

Excel Workbook
ABCDEFGHIJ
1DataTask 1 - StartTask 1 - EndTask 1 - CategoryTask 1 - StartTask 1 - EndTask 1 - Category
207:00 - 11:00 (On Duty) 11:00 - 11:30 (Lunch) 11:30 - 11:45 (Conference Call) 11:45 - 15:00 (On Duty)7:0011:00On Duty7:0011:30xyz
307:00 - 11:30 (xyz) 11:00 - 11:30 (Lunch) 11:30 - 11:45 (Call the President) 11:45 - 15:00 (On Duty) 15:15 - 15:30 (Break)Task 2 - StartTask 2 - EndTask 2 - CategoryTask 2 - StartTask 2 - EndTask 2 - Category
411:0011:30Lunch11:0011:30Lunch
5Task 3 - StartTask 3 - EndTask 3 - CategoryTask 3 - StartTask 3 - EndTask 3 - Category
611:3011:45Conference Call11:3011:45Call the President
7Task 4 - StartTask 4 - EndTask 4 - CategoryTask 4 - StartTask 4 - EndTask 4 - Category
811:4515:00On Duty11:4515:00On Duty
9Task 5 - StartTask 5 - EndTask 5 - Category
1015:1515:30Break
11
Tasks
 
Last edited:
Upvote 0
Forgot to mention that my suggestion also relies on the string " - " not occurring anywhere other than to separate the start/end time of a task.

(Mark: Thanks for the PM but from the single data sample given, I didn't see Regular Expressions as the best way to attack this one :))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,225
Members
452,896
Latest member
IGT

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