Excel VBA是否匹配?

Excel VBA是否匹配?,excel,vba,Excel,Vba,我有以下VBA代码: For q = 1 To pf.PivotItems.Count For i = LBound(myArray) To UBound(myArray) If pf.PivotItems(q).Name = myArray(i) Then same = True Next i pf.PivotItems(q).Visible = False If same = True Then

我有以下VBA代码:

For q = 1 To pf.PivotItems.Count
        For i = LBound(myArray) To UBound(myArray)
                If pf.PivotItems(q).Name = myArray(i) Then same = True
        Next i
        pf.PivotItems(q).Visible = False
        If same = True Then pf.PivotItems(q).Visible = True
        same = False

Next q
这一行给了我一个相等不匹配错误:
如果pf.PivotItems(q).Name=myArray(i)


为什么??如何修复此问题?

试试这个,我已经调整了代码以提高效率。正如评论中提到的,主要问题是myArray中有什么?它需要输出单个值,而不是数组或对象,因此您可能需要进一步限定myArray(i)


myArray是什么类型的数组?如何填充
myArray
?您是否尝试过使用
Debug.Print
或立即窗口来检查运行时的实际值?此外,您是否考虑过使用
Join
InStr
来完全替换循环和
相同的
?(
如果InStr(“|”&Join(myArray,“|”)和“|”、“|”&pf.PivotItems(q.Name&“|”)大于0,则pf.PivotItems(q.Visible=True
Dim pvtItm As PivotItem    'put this with other dim statements

For Each pvtItm In pf.PivotItems
    pvtItm.Visible = True
    For i = LBound(myArray) To UBound(myArray)
        If pvtItm.Name = myArray(i) Then
            pvtItm.Visible = False
            Exit For
        End If
    Next
Next