Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8990187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:26:46+00:00 2026-06-15T22:26:46+00:00

I am working on a macro in which I am putting together PivotTables. My

  • 0

I am working on a macro in which I am putting together PivotTables.

My Problem:
I can not get the calculation part to work. I get an error everytime I run the code at the stage where PivotField(18) is supposed to calculate the .Function=xlCount. The error I get is “1004 select method of range class failed”.

All the examples I’ve found online and in the MS Help section are structured the way I have set up my code. What am I missing???

Any help is highly appreciated…

I’ve been working on this the better part of the day. The following page (Link) has been super helpful, but hasn’t helped me in cracking this particular problem.

' Start of PivotSpecific Formatting
Set pt = ActiveSheet.PivotTables("PT" & wsNewWorksheetName)
With pt
    ' Prevent AutoFormatting of Columns
    .HasAutoFormat = False
    ' Hide Field Headers
    .DisplayFieldCaptions = False
    .TableStyle2 = "PivotStyleMedium2"
    ' Apply calculation to numbers and change Caption
    With .PivotFields(18)
        .Caption = "Demand"
        .Function = xlCount
        .NumberFormat = "#,##0"
    End With
    With .PivotFields(15)
        .Caption = "Lowest OP"
        .Function = xlMin
        .NumberFormat = "#,##0.00"
    End With
End With

Update 1: Screenshot of Pivot Table
Pivot Table Screenshot

Update 2: Tested Code from bonCodigo
Here is the code adjusted to my workbook. With this I get a run time error 91 on the line:
Set pvField = pvTable.PivotFields(18)

Dim wkSheet As Worksheet
Dim pvTable As PivotTable
Dim pvField As PivotField

Set wkSheet = ThisWorkbook.Worksheets(wsNewWorksheetName)
Set pvTble = wkSheet.PivotTables("PT" & wsNewWorksheetName)
Set pvField = pvTable.PivotFields(18)

With pvField
     .Caption = "Demand"
     .Function = xlCount
     .orientation = xlDataField
     .NumberFormat = "#,##0"
End With

Set pvField = Nothing
Set pvTable = Nothing
Set wkSheet = Nothing
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:26:47+00:00Added an answer on June 15, 2026 at 10:26 pm

    Try this please and comment with your results. So we can see from there.

    Option Explicit
    
    '-- whatever other code you may have... 
    
    Dim wkSheet as Worksheet
    Dim pvTable as PivotTable
    Dim pvField as PivotField
    
    Set wkSheet = ThisWorkbook.Worksheets("Sheet1") '-- user proper sheet name explicitly
    Set pvTble = wkSheet.PivotTables("PivotTable1") 
       ' or wkSheet.PivotTables(1) '-- use pivot table name or index
    Set pvField = pvTable.PivotFields("SomeField") '-- use pivot field name or index
    
    ' -- do whatever you want to do with the pivot table
    
    ' -- do what you need to do with the pivot field
    With pvField
         .Caption = "Demand"
         .Orientation = xlDataField '--<< THIS could well be the culprit
         .Function = xlCount
         .NumberFormat = "#,##0"
    End With
    
    Set pvField = Nothing
    Set PvTable = Nothing
    Set wkSheet = Nothing
    

    Since you have more than one field to format, I recommend you to use a loop if they are consecutive using DataFields Index

    e.g. pvTable.DataFields(1).Function = xlCount

    Otherwise you will have to define them separately. It is always best to assign an variable as a reference to the object that you intend to interact. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I put together some VBA code for Outlook 2007 which has been working predominantly
Some legacy code I am working on has a macro which returns a comma-separated
The first part is now working [ I have the following which just seems
I am working on a VSTO Excel 2010 Macro Enabled Template Project, that I
I am working on a helper macro that look into the list function on
hi i am working with c# and excel-2007... i wanted to add a macro
Working with an undisclosed API, I found a function that can set the number
This is pasted from a website, which presumably was working. I did some googling
I'm attempting to write a macro which will call java setter methods based on
I am working on PowerPoint macro. In that I want to apply theme programatically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.