使用常量值更新VBA中的数据透视表周数

时间:2018-03-29 14:50:56

标签: excel vba excel-vba

我们正在尝试让Excel根据两个单元格中的用户输入更新数据透视表的值。它应该更新代码中的部分2016014和2017014.(014是周数)。我们指示用户在单元格AD和AE中使用YYYYWWW格式输入周数(2016年为1,2017年为1)。我们如何让Excel从这些输入中读取值?

我们会宣布这样的事吗?唯一的问题是我得到了

  

Sub或Function

中的属性无效
        Public Const weekNumber1 As String = ThisWorkbook.Sheets(Charts).Cells(30, 4).Value
        Public Const weekNumber2 As String = ThisWorkbook.Sheets(Charts).Cells(31, 4).Value

以下是实际代码:

Sub WeekUpdate(Optional ByVal ws As Worksheet)
                        If ws Is Nothing Then Set ws = ActiveSheet
            ws.PivotTables("PivotTable6").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Year]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable6").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Qtr]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable6").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Period]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable6").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Week]").VisibleItemsList = Array( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Week].&[2016014]")
            ws.PivotTables("PivotTable6").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Date]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable5").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Year]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable5").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Qtr]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable5").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Period]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable5").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Week]").VisibleItemsList = Array( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Week].&[2017014]")
            ws.PivotTables("PivotTable5").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Date]").VisibleItemsList = Array("")

以下是代码的第二部分(它称为第一部分):

End Sub

Sub DoSheets()

Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet

For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Charts" Then
        WeekUpdate ws
        ws.Cells(1, 1) = 1
    End If
Next

starting_ws.Activate

End Sub

更新代码

Public Function weekNumber1() As String
    weekNumber1 = ThisWorkbook.Sheets("Charts").Cells(4, 30).Value
End Function
Public Function weekNumber2() As String
    weekNumber2 = ThisWorkbook.Sheets("Charts").Cells(5, 30).Value
End Function


Sub WeekUpdate(Optional ByVal ws As Worksheet)
            If ws Is Nothing Then Set ws = ActiveSheet
            ws.PivotTables("PivotTable6").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Year]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable6").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Qtr]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable6").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Period]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable6").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Week]").VisibleItemsList = Array( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Week].&[weekNumber1()]")
            ws.PivotTables("PivotTable6").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Date]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable5").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Year]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable5").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Qtr]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable5").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Period]").VisibleItemsList = Array("")
            ws.PivotTables("PivotTable5").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Week]").VisibleItemsList = Array( _
                "[Date].[Fiscal Date Hierarchy].[Fiscal Week].&[weekNumber2()]")
            ws.PivotTables("PivotTable5").PivotFields( _
                "[Date].[Fiscal Date Hierarchy].[Date]").VisibleItemsList = Array("")
End Sub


Sub DoSheets()

Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet

For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Charts" Then
        WeekUpdate ws
        ws.Cells(1, 1) = 1
    End If
Next

starting_ws.Activate

End Sub

1 个答案:

答案 0 :(得分:0)

由于无法使用变量值声明常量,因此一种解决方案是使用公共函数 - 没有参数 - 提供所需的值,例如:

Public Function weekNumber1() As String
    weekNumber1 = ThisWorkbook.Sheets("Charts").Cells(30, 4).Value
End Function

Public Function weekNumber2() As String
    weekNumber2 = ThisWorkbook.Sheets("Charts").Cells(31, 4).Value
End Function

您可以在代码中调用/使用它,就像使用常量一样。

由于您正在使用字符串中的值,因此您应该更改它:

ws.PivotTables("PivotTable5").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Week]").VisibleItemsList = Array( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Week].&[weekNumber2()]")

到此:

ws.PivotTables("PivotTable5").PivotFields( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Week]").VisibleItemsList = Array( _
    "[Date].[Fiscal Date Hierarchy].[Fiscal Week].&[" & weekNumber2() & "]")

您还应该更新引用weekNumber1()

的字符串

编辑

的说明

你需要以它正在寻找的格式提供刺痛。但是,要在字符串中调用Function(或使用变量'value),您需要将字符串的各个部分附加在一起。如果您将函数(或变量)名称包含在字符串中,VBA将使用您包含的文字文本而不是替换正确的值。

<强>被修改

Charts更改为"Charts",因为Charts是工作表名称