Help With Code

Desu Nota from Columbus

Well-known Member
Joined
Mar 17, 2011
Messages
556
I have the following code where everything works except for the pivot table.

The pivot table is not created, I think this is because I don't have the Table Destination correct (it is asking for me to specify the [workbook] but i'm not sure how to say "this workbook"). Also, I have a dynamic pivot table source data (the amount of rows change, but the columns DO NOT). How would I set the source range to select all rows with data?

This macro runs within one workbook (with multiple sheets) and I am taking data from sheet16, and putting a pivot table on sheet Results! F3.

Rich (BB code):
With Sheets("Sheet16")
    Columns("A:N").NumberFormat = "General"
    Range("A:A,G:G,L:L").NumberFormat = "m/d/yyyy h:mm"
    Range("D:D,H:J,M:N").Delete
    Columns("F:F").Select
    Selection.Cut
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight
    Columns("H:H").Select
    Selection.Cut
    Columns("D:D").Select
    Selection.Insert Shift:=xlToRight
    Columns("G:G").Select
    Selection.Cut
    Columns("I:I").Select
    Selection.Insert Shift:=xlToRight
    Cells.Select
    Selection.Columns.AutoFit
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown
    Range("A1").Value = "CaptureTime"
    Range("B1").Value = "DeviceKey"
    Range("C1").Value = "Start Time"
    Range("D1").Value = "End Time"
    Range("E1").Value = "Duration"
    Range("F1").Value = "Reason"
    Range("G1").Value = "State"
    Range("H1").Value = "Sequence Number"
    Range("A1,B1,C1,D1,E1,F1,G1,H1").Select
            With Selection.Font
                .FontStyle = "Bold"
            End With
    Cells.AutoFilter
    Selection.AutoFilter Field:=7, Criteria1:="run_enum"
    Columns("A:A").Select
    Range("A" & Rows.Count).End(xlUp).Select
    Range(Selection, Selection.End(xlUp)).Offset(1, 0).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Delete Shift:=xlUp
    On Error Resume Next
    Selection.AutoFilter Field:=7, Criteria1:="standby_enum"
    Columns("A:A").Select
    Range("A" & Rows.Count).End(xlUp).Select
    Range(Selection, Selection.End(xlUp)).Offset(1, 0).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Delete Shift:=xlUp
    On Error Resume Next
    Selection.AutoFilter Field:=7, Criteria1:="setup_enum"
    Columns("A:A").Select
    Range("A" & Rows.Count).End(xlUp).Select
    Range(Selection, Selection.End(xlUp)).Offset(1, 0).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Delete Shift:=xlUp
    On Error Resume Next
    Selection.AutoFilter Field:=7, Criteria1:="offline_enum"
    Columns("A:A").Select
    Range("A" & Rows.Count).End(xlUp).Select
    Range(Selection, Selection.End(xlUp)).Offset(1, 0).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Delete Shift:=xlUp
    On Error Resume Next
    Cells.AutoFilter
    Range("BB1").Value = 86400
    Range("BB1").Copy
    Range("E2:E65534").PasteSpecial xlPasteValues, xlPasteSpecialOperationDivide
    Columns("E:E").NumberFormat = "[h]:mm:ss"
    Columns("BB:BB").Delete
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "Sheet16!R1C1:R152C9").CreatePivotTable TableDestination:= _
        "Results!R4C6", TableName:="PivotTable1", DefaultVersion:= _
        xlPivotTableVersion10
    ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="Reason", _
        ColumnFields:="DeviceKey"
End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Duration")
        .Orientation = xlDataField
        .Caption = "Sum of Duration"
        .Function = xlSum
        .NumberFormat = "[h]:mm:ss"
    End With
Ive posted code above the area of interest so you could see how the With statements are setup (if they are the problem).

The problem areas are in BOLD BLUE.

Thanks in advance.
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,224,586
Messages
6,179,710
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