为什么VBA数组不加载整数

这里是我在网上find的一个函数,可以帮助循环查看值并select与值相匹配的透视筛选器项目。 我的问题是在Sub Filter_Bana中创build的数组不会加载名为“Bana”(又名varItemList)范围内的值。 范围“Bana”由大约二十个数字(整数)组成。 当我运行子(在底部),我不断收到函数MsgBox“没有findfilter列表项”。 我一直试图弄清楚这一点,并不认为任何命名的整数列表“Bana”正在加载到“varItemList”。 换句话说,当varItemList传递给函数时,数组是空的。 请参阅代码:

**编辑:我发现这个问题。 我遇到的问题涉及到两个问题:1)我在VBA不好,2)我的数据透视表字段中的数据types与数据types不匹配。 我将数组组件转换为5个字符的文本,并调整了SQL查询,将我的investorNumber作为5个字符的文本(即数组需要加载string,而我的透视字段需要字符types的数据;如果有办法用整数来做到这一点,我很想知道。

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` 

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

 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