Edit Macro Code to reference law row & last column on a tab instead of a fixed reference

VorLag

Board Regular
Joined
May 13, 2013
Messages
205
Hello, fellow Excel-geeks!

I am trying to get a Macro working to create a pivot table for me, but I am having trouble with getting it to look for whatever the last column and last row are. I have been poking around online and have tried a few different things, but I either don't understand the code that those other sites are using and I'm using it wrong, or there is a different method that I need to use.

So, my worksheet has 2 tabs - Details, with all of the raw data, and Pivot, which has nothing right now, but should have the pivot table that references the Details tab. My data on the Details tab begins at Cell A6 and in this set of data, it ends with the last cell of Column AC. However, the number of Columns may change and the number of Rows will definitely change.

I can live with it always starting with A6, because that part is easy enough to change if/when needed.

So, I found the first part of this code elsewhere online and put it in. When I try to run this Macro, I get this error message:
Unable to get the PivotTables property of the Worksheet class

I have no idea what that means. :)

Here is the code for my macro:

Code:
    ActiveWorkbook.Worksheets("Details").Range("A6").Select
    Selection.CurrentRegion.Select
    DataArea = "Details!R6C1:R" & Selection.Rows.Count + 5 & "C" & Selection.Columns.Count
 
    ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataArea, _
        Version:=xlPivotTableVersion14)

    Sheets("Pivot").Select
    Cells(1, 1).Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Duplicate")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Job Category")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Job Status")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("SLA Time to Shortlist" _
        )
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Job ID"), "Sum of Job ID", xlSum
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Job ID")
        .Caption = "Count of Job ID"
        .Function = xlCount
    End With
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Duplicate").CurrentPage = _
        "(All)"
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Duplicate")
        .PivotItems("Duplicate").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Duplicate"). _
        EnableMultiplePageItems = True
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Job Category").CurrentPage _
        = "(All)"
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Job Category")
        .PivotItems("Call Center").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Job Category"). _
        EnableMultiplePageItems = True
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Job Status").CurrentPage = _
        "(All)"
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Job Status")
        .PivotItems("Canceled").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Job Status"). _
        EnableMultiplePageItems = True
End Sub

ALSO,

Does anyone know how I might add some conditional language into the actual pivot table code? For example, it is expected that there will always be Duplicates, so no condition is needed there, but there may not be any Call Center data or there may not be any Canceled jobs in the data.

I get an error in the code if that is the case, but I need the Macro to always filter Call Center and Canceled jobs out of the pivot table IF they exist in the data. How can I do that?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

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