【问题标题】:Why is VBA array not loading integers为什么VBA数组不加载整数
【发布时间】:2015-12-09 00:00:34
【问题描述】:

这是我在网上找到的一个函数,可帮助循环遍历值并选择与值匹配的枢轴过滤器项目。我的问题是在 Sub Filter_Bana 中创建的数组没有加载名为“Bana”(又名 varItemList)的范围内的值。范围“Bana”由大约二十个数字(整数)组成。当我运行子程序(在底部)时,我不断收到来自函数的 MsgBox“未找到过滤器列表项”。我一直试图弄清楚这一点,并且不认为任何命名的整数列表“Bana”被加载到“varItemList”中。换句话说,当 varItemList 被传递给函数时,数组是空的。请看代码:

**编辑:我发现了问题。我遇到的问题与两个问题有关:1)我不擅长 VBA,2)我的数据透视字段中数据透视项的数据类型与数组的数据类型不匹配。我将数组组件切换为 5 个字符的文本并调整 SQL 查询以将我的investorNumber 作为 5 个字符的文本引入(即数组需要加载字符串,而我的数据透视字段需要字符类型的数据;如果有办法用整数做到这一点,我很想知道。**

Private Function Filter_PivotField(pvtField As PivotField, _
    varItemList As Variant)
    Dim strItem1 As Long, blTmp As Boolean, i As Long
    On Error Resume Next
    Debug.Print varItemList
    Application.ScreenUpdating = False
    With pvtField
        If .Orientation = xlPageField Then .EnableMultiplePageItems = True
        For i = LBound(varItemList) To UBound(varItemList)
            blTmp = Not (IsError(.PivotItems(varItemList(i)).Visible))
            If blTmp Then
                strItem1 = .PivotItems(varItemList(i))
                Exit For
            End If
        Next i
        If strItem1 = "" Then
            MsgBox "None of filter list items found."
            Exit Function
        End If
        .PivotItems(strItem1).Visible = True
        For i = 1 To .PivotItems.Count
            If .PivotItems(i) <> strItem1 And _
                .PivotItems(i).Visible = True Then
                .PivotItems(i).Visible = False
            End If
        Next i
        For i = LBound(varItemList) To UBound(varItemList)
            .PivotItems(varItemList(i)).Visible = True
        Next i
    End With
  Application.ScreenUpdating = True
End Function


Sub Filter_Bana()
    Filter_PivotField _
        pvtField:=Sheets("Pres1&2_Pivot").PivotTables("PivotTable1").PivotFields("investorNumber"), _
        varItemList:=Application.Transpose(Sheets("Controls").Range("Bana"))

End Sub`

【问题讨论】:

  • 首先停止这样的事情 - 摆脱 On Error Resume Next 声明,实际上是 debug your code
  • Debug.Print varItemList 你不能调试。打印这样的数组
  • 对不起宏人。这是一个很好的观点。
  • 是的,我已经摆脱了 debug.print 的东西。这是我试图弄清楚数组中是否有任何东西时的残余。删除“on Error Resume Next”后,我得到的错误是“无法获取 PivotField 类的 pivotItems 属性。有什么想法吗?
  • Debug.Print Join(varItemList, ", ") 是在即时窗口中查看该数组内容的一种有用的快速方法。

标签: vba excel pivot-table


【解决方案1】:

此代码有效。唯一的另一个关键是确保我的数组加载了字符类型数据(不是整数),并且数据透视字段还包含字符类型数据(在查询中调整/不显示)

Private Function Filter_PivotField(pvtField As PivotField, _
    varItemList As Variant)
    Dim strItem1 As String, blTmp As Boolean, i As Long
    Application.ScreenUpdating = False
    With pvtField
        If .Orientation = xlPageField Then .EnableMultiplePageItems = True
        For i = LBound(varItemList) To UBound(varItemList)
            blTmp = Not (IsError(.PivotItems(varItemList(i)).Visible))
            If blTmp Then
                strItem1 = .PivotItems(varItemList(i))
                Exit For
            End If
        Next i
        If strItem1 = "" Then
            MsgBox "None of filter list items found."
            Exit Function
        End If
        .PivotItems(strItem1).Visible = True
        For i = 1 To .PivotItems.Count
            If .PivotItems(i) <> strItem1 And _
                .PivotItems(i).Visible = True Then
                .PivotItems(i).Visible = False
            End If
        Next i
        For i = LBound(varItemList) To UBound(varItemList)
            .PivotItems(varItemList(i)).Visible = True
        Next i
    End With
  Application.ScreenUpdating = True
End Function


Sub Filter_Bana()
Filter_PivotField _
    pvtField:=ThisWorkbook.Worksheets("Pres1_2_Pivot").PivotTables("PivotTable1").PivotFields("investorNumber"), _
    varItemList:=Application.Transpose(Sheets("Controls").Range("Bana"))

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    相关资源
    最近更新 更多