Converting Messy Data to Pivot Table

redhots4

Board Regular
Joined
Aug 30, 2004
Messages
136
Office Version
  1. 365
Platform
  1. MacOS
This is a typical report exported into Excel. How do I take data formatted like this and massage it to align in nice, neat columns like the file far below? This is only a sample of the raw data file. In the real world, I woulc have perhaps 25 projects (unique SR numbers) and 50-60 resources, sometimes showing hours worked on multiple projects / SRs.

BEFORE:
Book1.xls
ABCDE
6ManagerA.Manager
7SRWS060003
8RESOURCEDuck,Donald
9
10Planning0
11Analysis10
12Design20
13Construction10
14Testing0
15Deployment0
16
17RESOURCEMouse,Mickey
18
19Planning0
20Analysis0
21Design35
22Construction10
23Testing0
24Deployment0
25
26SRWS060004
27RESOURCEGoofy,Dawg
28
29Planning0
30Analysis25
31Design10
32Construction10
33Testing0
34Deployment0
Sheet1



AFTER:
Book1.xls
ABCDEFGHI
1SRResourceDateEndPlanningAnalysisDesignConstructionTestingDeployment
2WS060003Duck,Donald8/8/2006010201000
3WS060003Mouse,Mickey8/8/200600351000
4WS060004Goofy,Dawg8/8/2006025101000
5
Sheet2
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi

The following code will manipulate the data on the current sheet, then build a pivot table starting in F6.

Code:
Sub ccc()
  Range("A6").Value = "SR"
  Range("B6").Value = "RESOURCE"
  Range("D6").Value = "DEPARTMENT"
  Range("E6").Value = "HOURS"
  Range("A:A").Replace what:="SR ", replacement:=""
  Range("A:A").Replace what:="RESOURCE", replacement:=""
  Columns("C:C").Delete
  lastrow = Cells(Rows.Count, 3).End(xlUp).Row
  Range("A8:A" & lastrow).SpecialCells(xlCellTypeBlanks).Formula = "=A7"
  Range("B9:B" & lastrow).SpecialCells(xlCellTypeBlanks).Formula = "=B8"
  Range("A:B").Value = Range("A:B").Value
  For i = lastrow To 7 Step -1
    If IsEmpty(Cells(i, 3)) Then Cells(i, 3).EntireRow.Delete
  Next i
  lastrow = Cells(Rows.Count, 3).End(xlUp).Row
  
  ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        ActiveSheet.Name & "!R6C1:R" & lastrow & "C4").CreatePivotTable TableDestination:=ActiveSheet.Name & "!R6C6" _
        , TableName:="Output", DefaultVersion:=xlPivotTableVersion10
    With ActiveSheet.PivotTables("Output").PivotFields("SR")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("Output").PivotFields("RESOURCE")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("Output").PivotFields("DEPARTMENT")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("Output").AddDataField ActiveSheet.PivotTables( _
        "Output").PivotFields("HOURS"), "Sum of HOURS", xlSum
    
    ActiveSheet.PivotTables("Output").PivotFields("RESOURCE").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    
    ActiveSheet.PivotTables("Output").PivotFields("SR").Subtotals = Array( _
        False, False, False, False, False, False, False, False, False, False, False, False)
    
    With ActiveSheet.PivotTables("Output")
        .ColumnGrand = False
        .RowGrand = False
    End With
  
  
End Sub

HTH

Tony
 
Upvote 0
also try;
Code:
Sub test()
Application.ScreenUpdating = False
Columns("c:d").Insert shift:=xlToRight
Dim i, ii As Long
For i = 6 To Range("b" & Rows.Count).End(xlUp).Row
If Cells(i, "a") Like "SR*" Then
Cells(i, "c") = Cells(i, "a")
End If
If Cells(i, "a") = "RESOURCE" Then
Cells(i, "d") = Cells(i, "b")
End If
Cells(i, "e") = 1
Next

Range("e7").Activate
Do While ActiveCell.Value<> ""
If ActiveCell.Offset(1, -2) = "" Then
ActiveCell.Offset(1, -2).Value = ActiveCell.Offset(, -2).Value
End If

If ActiveCell.Offset(1, -1) = "" Then
ActiveCell.Offset(1, -1).Value = ActiveCell.Offset(, -1).Value
End If

ActiveCell.Offset(1).Activate
Loop

'Range("e7").Activate
'Do While ActiveCell.Value<> ""
'If ActiveCell.Offset(1, -1) = "" Then
'ActiveCell.Offset(1, -1).Value = ActiveCell.Offset(, -1).Value
'End If
'ActiveCell.Offset(1).Activate
'Loop

For ii = Range("c" & Rows.Count).End(xlUp).Row To 6 Step -1
If Cells(ii, "f") = "" Then
Rows(ii).Delete
End If
Next
Columns("e").Delete
Range("b5").Resize(, 4) = Array("Analysis", "SR", "Resource", "Qty")


Range("B5:E7").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "Sheet3!R5C2:R23C5").CreatePivotTable TableDestination:="", TableName:= _
        "PivotTable2"
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    ActiveSheet.Cells(3, 1).Select
    ActiveSheet.PivotTables("PivotTable2").SmallGrid = False
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("SR")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Resource")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Analysis")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Qty")
        .Orientation = xlDataField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable2").PivotFields("SR").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
    With ActiveSheet.PivotTables("PivotTable2")
        .ColumnGrand = False
        .RowGrand = False
    End With
Application.ScreenUpdating = True
End Sub
which gives you a result like this in new sheet;
Book1
ABCDEFGH
3Sum of QtyAnalysis
4SRResourceAnalysisConstructionDeploymentDesignPlanningTesting
5SR WS060003Duck, Donald101002000
6Mouse, Mickey01003500
7SR WS060004Goofy, Dawg251001000
Sheet18
 
Upvote 0
Realized that the data layout is slightly different than I posted originally. How does this change the macro? Note all of the jazz in the first column.
TINKERING.xls
ABCD
1
2REPORTCRITERIASRPhaseDetail(Resource)
3
4Division=DivisionName
5Inputhourlyrate=$55
6From08/13/2006to08/19/2006
7
8SRNumber=WS060003ACTUALPLANNED
9BusinessDept=ALL,CostCenter=ALL
10HoursCostHours
11SRDEPT(CostAllocationDept)DepartmentName
12SRTYPE2-Strategic
13SROBJECTIVEProject
14SRWS060003Project_Name_HereSRStatus:
15
16CCDEPARTMENTCCNAMECCID
17DepartmentNameTeamNameB202
18
19RESOURCELast1,First1
20Definition
21Analysis
22Design10.00$550.00
23Coding20.00$1,100.00
24Testing10.00$550.00
25Dcmnt/Training
26Implement/Post
27ProjectAdmin.
28Maintenance
29TOTALforLast1,First140.00$2,200.00
30
Sheet1
 
Upvote 0
Hi

Bit more than "slightly different"...

Does this example cover all the struture / headings that will occur in a full report?


Tony
 
Upvote 0
Yes, the top rows willbe exactly the same through row 9. Starting in ROW 11, the report will show a heading by SR (Project Number) and then relect resource names and proejct phases in column A. Imagine that in row 31, what you see in rows 11 to 30 will repeat with a different SR and the same or different resource names.

Part of what I'm after here is a standard process used by managers and reporting personnell to extract useful, columned data from reports formatted this way. I need to have predictable columns of data so I can pivot it the way i'd like. I'm sure I'm not the first guy to run aross this issue.
 
Upvote 0
redhots4 -

this codes manipulate the data.

Code:
Sub test()
Dim i, ii, iii, lr As Long
Application.ScreenUpdating = False
Range("e13").Resize(, 4) = Array("SR", "Name", "Hours", "Cost")
For i = 14 To Range("A" & Rows.Count).End(xlUp).Row
If Cells(i, "a") = "SR" Then
Cells(i, "e") = Cells(i, "b")
End If
If Cells(i, "a") = "RESOURCE" Then
Cells(i, "f") = Cells(i, "b")
End If

If Cells(i, "b")<> "" Then
Cells(i, "g") = Cells(i, "b")
End If

If Cells(i, "c")<> "" Then
Cells(i, "h") = Cells(i, "c")
End If

Next
Columns("b:d").Delete
Rows("1:12").Delete
[a1] = "Resource"

For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
If Cells(i, "b")<> Cells(i, "b").Offset(1) And Cells(i, "b").Offset(1) = "" Then
Cells(i, "b").Offset(1) = Cells(i, "b")
End If

If Cells(i, "c")<> Cells(i, "c").Offset(1) And Cells(i, "c").Offset(1) = "" Then
Cells(i, "c").Offset(1) = Cells(i, "c")
End If

Next

For iii = Range("b" & Rows.Count).End(xlUp).Row To 2 Step -1
If Cells(iii, "a").Value Like "*Department*" Then
Rows(iii).Delete
End If
If Cells(iii, "a").Value = "TOTAL for" Then
Rows(iii).Delete
End If
If Cells(iii, "a").Value = "RESOURCE" Then
Rows(iii).Delete
End If
If Cells(iii, "a").Value = 0 Then
Rows(iii).Delete
End If
If Cells(iii, "a").Value Like "SR*" Then
Rows(iii).Delete
End If
Next
Cells.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
result will be like this, from here you can do a pivot table.
Book1
ABCDE
1ResourceSRNameHoursCost
2DefinitionWS060003Last1, First1
3AnalysisWS060003Last1, First1
4DesignWS060003Last1, First110.00$550.00
5CodingWS060003Last1, First120.00$1,100.00
6TestingWS060003Last1, First110.00$550.00
7Dcmnt/TrainingWS060003Last1, First1
8Implement/PostWS060003Last1, First1
9Project Admin.WS060003Last1, First1
10MaintenanceWS060003Last1, First1
11DefinitionWS060004Last2, First2
12AnalysisWS060004Last2, First2
13DesignWS060004Last2, First210.00$550.00
14CodingWS060004Last2, First220.00$1,100.00
15TestingWS060004Last2, First210.00$550.00
16Dcmnt/TrainingWS060004Last2, First2
17Implement/PostWS060004Last2, First2
18Project Admin.WS060004Last2, First2
19MaintenanceWS060004Last2, First2
Sheet1
 
Upvote 0

Forum statistics

Threads
1,226,223
Messages
6,189,719
Members
453,566
Latest member
ariestattle

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