Create calculated field outside of Pivot table using VBA

BenChod1

New Member
Joined
Apr 18, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all-

Am pretty new to VBA and hoping to get some assistance from the experts. I have a worksheet with a large range of data. From the worksheet, I am able to create a pivot table using code. For the row, there is one field and for the values, I am using the count option. For the row, the data is in seconds and for the value, it gives me the count of instances for the number of seconds. What I want to do is the following: in the adjacent colum next the the values, I want to convert seconds to the following format: h:mm:ss by dividing the number of seconds by 86400 and convert the format to h:mm:ss. I want the formula to run on the entire data set in the pivot table. Here is the code I came up with..

Code:

'Insert Data Field
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("2. ACE requests Lock and Confirm Eligibility, per Throttle Rate")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
.Name = "BAN Count "
End With

Set rng = Range("D3:D1000")
For Each cell In rng

Range("D3").Select
ActiveCell.Formula = "=B3/86400"
Selection.NumberFormat = "[h]:mm:ss;@"

Next cell
Code:

As you can see, I can't figure out how to move to the next cell and convert seconds to time format. Attached is a pic of my pivot table. Again, hoping one of the experts can assist. Thanks for the help

PivotVBA.PNG
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,215,603
Messages
6,125,788
Members
449,260
Latest member
Mrw1

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