将固定值动态应用于数据透视表中的计算字段 table

Apply a fixed value dynamically to a calculated field in a pivot table

我有以下 Excel 电子表格:

C 列 中,您可以从B 列 中的products 中看到sales。在 A 列 中,您可以找到 B 列中每个 products 对应的 brand

基于这些数据,我创建了以下数据透视表:


在我的数据透视表中,我使用以下公式创建了一个名为 sales per day 计算字段

这准确地给出了我需要的结果,但如您所见,我输入了 天数 (在本例中为 360)作为固定数字进入计算字段的函数。

但是,我宁愿不要输入这个数字作为固定汇率,并在我的数据透视表中灵活设置,以便用户更改 Cell F1 中的数字database 中,它会自动正确地应用于数据透视表。

你知道我该如何解决这个问题吗?
有没有我可以使用的 帮助列

抱歉,数据透视表描述只有德语版。

将此代码放入作品sheet的私有代码sheet(右键单击,查看代码)而不是public模块代码sheet。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Count > 1 Then Exit Sub

    If Target.Address = "$F" And CBool(Len(Target.Value2)) Then
        On Error GoTo safe_exit
        Application.EnableEvents = False
        If IsNumeric(Target.Value2) Then
Debug.Print Target.Address
            Me.PivotTables("PivotTable1"). _
               CalculatedFields("sales per day").StandardFormula = _
               "=sales/" & Target.Value2
        End If
    End If

safe_exit:
    Application.EnableEvents = True

End Sub

您可能需要调整一些识别名称。现在,每当您在 F1 中键入一个新的数值时,主元 table 的计算 'sales per day' 字段将有一个新的公式。