作者:聆听最遥远的歌声 | 来源:互联网 | 2023-05-19 13:33
Icurrentlyhaveafunctionsetinplacewhichwillchangeallvaluesofaselectedpivottabletoa
I currently have a function set in place which will change all values of a selected pivot table to average.
我目前有一个功能集,它将所选枢轴表的所有值更改为平均值。
It works fine, and I have assembled a form which passes a value which works fine as well.
它运行正常,我已经组装了一个表单,该表单传递的值也可以正常工作。
What I would like to do at this point is make it so that it decides what to turn the values to.
在这一点上我想做的是让它决定将值转换为什么。
I however, keep getting a Type-Mismatch
error. This is because it is being read as a string. How could I go along of adjusting this?
但是,我一直收到类型不匹配错误。这是因为它被读作字符串。我怎么能调整这个呢?
Private Sub CommandButton1_Click()
MsgBox xl & ListBox1.Value
Dim ptf As Excel.PivotField
With Selection.PivotTable
.ManualUpdate = True
For Each ptf In .DataFields
With ptf
.Function = "xl" & ListBox1.Value 'xlAverage works here
.NumberFormat = "#,##0"
End With
Next ptf
.ManualUpdate = False
End With
End Sub
Private Sub ListBox1_Click()
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize() 'Set Values Upon Opening
With ListBox1
.AddItem "Sum"
.AddItem "Count"
.AddItem "Average"
.AddItem "Max"
.AddItem "Min"
.AddItem "Product"
.AddItem "CountNumbers"
.AddItem "StdDev"
.AddItem "StdDevp"
.AddItem "Var"
.AddItem "Varp"
End With
End Sub
3 个解决方案
3
Of course, the name of a variable at compile time, such as xlAverage
, is not necessarily reported to the run-time, where it is simply an integer constant. After compilation, the constants names are gone.
当然,编译时变量的名称,例如xlAverage,不一定会报告给运行时,它只是一个整数常量。编译后,常量名称消失了。
There are many ways around this, but they might go from the complicated use of type libraries which moreover are not available on all platforms, to the relatively simple solution I suggest below, which uses a dictionary to track the mappings between the names of the constants and their values.
有很多方法可以解决这个问题,但是它们可能会从复杂的类型库中使用,而这些类型库在所有平台上都不可用,而是我在下面建议的相对简单的解决方案,它使用字典来跟踪常量名称之间的映射。和他们的价值观。
Private Sub CommandButton1_Click()
' ...
ptf.Function = GetEnumConsolidationFunction.item(ListBox1.Value)
' ...
End Sub
Private Sub UserForm_Initialize()
Dim dict As Object, s
Set dict = GetEnumConsolidationFunction
For Each s In dict.Keys
ListBox1.AddItem s
Next
End Sub
' Our key function, fills a dictionary first time it is used
Function GetEnumConsolidationFunction() As Object
Static dict As Object '<-- static because we want to fill it only once
If dict Is Nothing Then
Set dict = CreateObject("Scripting.Dictionary")
With dict
.Add "Sum", XlConsolidationFunction.xlSum
.Add "Count", XlConsolidationFunction.xlCount
.Add "Average", XlConsolidationFunction.xlAverage
.Add "Max", XlConsolidationFunction.xlMax
.Add "Min", XlConsolidationFunction.xlMin
.Add "Product", XlConsolidationFunction.xlProduct
.Add "CountNums", XlConsolidationFunction.xlCountNums
.Add "StDev", XlConsolidationFunction.xlStDev
.Add "StDevp", XlConsolidationFunction.xlStDevP
.Add "Var", XlConsolidationFunction.xlVar
.Add "Varp", XlConsolidationFunction.xlVarP
End With
End If
Set GetEnumCOnsolidationFunction= dict
End Function
By the way, in this method you are not obliged to map the same names as of the variables. You are free map any names you want to display in the list box; i.e. "Mimimum", "Standard Deviation", etc..
顺便说一句,在这种方法中,您没有义务映射与变量相同的名称。您可以自由映射要在列表框中显示的任何名称;即“Mimimum”,“Standard Deviation”等。
p.s.
附:
Please be aware that you had some typos in the names:
请注意,您的名字中有一些拼写错误:
CountNumbers
--> CountNums
CountNumbers - > CountNums
StdDev
--> StDev
StdDev - > StDev
StdDevp
--> StDevP
StdDevp - > StDevP