Reworking code

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
There has to be an easier way, using Range("G9:G13") rather than individual cells...but I can't get it.

Code:
    Range("G9").Select
    For i = 0 To 13
        With ActiveCell
            .Offset(0, i) = PivotTable.GetPivotData("Sum of OT Hours", "Pay Period", .Offset(-6, i), "Office", Range("A9")) / PivotTable.GetPivotData("Sum of Total Hours", "Pay Period", .Offset(-6, i), "Office", Range("A9"))
            .Offset(1, i) = PivotTable.GetPivotData("Sum of OT Hours", "Pay Period", .Offset(-6, i), "Office", Range("A10")) / PivotTable.GetPivotData("Sum of Total Hours", "Pay Period", .Offset(-6, i), "Office", Range("A10"))
            .Offset(2, i) = PivotTable.GetPivotData("Sum of OT Hours", "Pay Period", .Offset(-6, i), "Office", Range("A11")) / PivotTable.GetPivotData("Sum of Total Hours", "Pay Period", .Offset(-6, i), "Office", Range("A11"))
            .Offset(3, i) = PivotTable.GetPivotData("Sum of OT Hours", "Pay Period", .Offset(-6, i), "Office", Range("A12")) / PivotTable.GetPivotData("Sum of Total Hours", "Pay Period", .Offset(-6, i), "Office", Range("A12"))
            .Offset(4, i) = PivotTable.GetPivotData("Sum of OT Hours", "Pay Period", .Offset(-6, i), "Office", Range("A13")) / PivotTable.GetPivotData("Sum of Total Hours", "Pay Period", .Offset(-6, i), "Office", Range("A13"))
        End With
    Next i
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
My thought/wonder is if the code can be reworked removing the .offset(-6,1) and replacing with something like "R3C" ....or does that only work when using Range("G9:T13").FormulaR1C1 = ?
 
Upvote 0
I was able to use the R1C1 style as follows...

Code:
Range("G9:T13").FormulaR1C1 = "=GETPIVOTDATA(""Sum of OT Hours"",Pivot!R1C1,""Pay Period"",DATE(YEAR(R3C),MONTH(R3C),DAY(R3C)),""Office"",RC1)/GETPIVOTDATA(""Sum of Total Hours"",Pivot!R1C1,""Pay Period"",DATE(YEAR(R3C),MONTH(R3C),DAY(R3C)),""Office"",RC1)"
Range("G9:T13") = Range("G9:T13").Value
It saves time, but still does some double work by setting the same range twice.

Is it possible to not have to use the second line to put just the value in the cells?
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

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