0

I'm trying to create custom reports using VBA created pivot tables and one of the parts I am having trouble with is setting the datafield to be based on what he user desires.

For example: I have a drop down menu that displays each type of PivotFields function (sum, min max etc.) and depending on what the user selects it is stored as a string variable.

When I run my code:

Dim valueXL As String
valueXL = xlSum

With Worksheets(sheetPivotName).PivotTables(sheetPivotName).PivotFields(valueType)
.Function = valueXL
.NumberFormat = "$#,##0.00"
End With

I get an error, however, if I replace .Function = valueXL with .Function = xlSum it works correctly.

Is there any way to dynamically set the .Function value?

Thanks

2 Answers 2

3

As xlSum is a numeric Excel constant you should change valueXL to integer

Dim valueXL As Integer
valueXL = xlSum

or use direct the constant in your code

With Worksheets(sheetPivotName).PivotTables(sheetPivotName).PivotFields(valueType)
  .Function = xlSum
  .NumberFormat = "$#,##0.00"
End With
Sign up to request clarification or add additional context in comments.

2 Comments

or use CInt(valueXL)... still it makes no sense that valueXL is defined as string :/
Thanks for this , I had no idea xlSum and those values were treated as integers and not strings!
0

I found a possible solution, which is using the Evaluate function, however I was wondering if there were any other solutions?

If not I can select this as the answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.