我正在使用Excel 365,并且正在使用VBA在某些数据透视表中进行查找。当我过滤某些内容然后执行.GetData查询数据透视表时,遇到了一个问题,而我正在查找的内容不再存在。例如,如果我要设置数据透视表,以使Item_B不再位于其中,然后执行以下操作:
pt.GetData("Col_1 Item_B")
我会得到错误。我尝试做类似以下的事情,但是它给出了类型不匹配的错误(我实际上是基于label和i.Value来获取数据,而i.Value是单元格范围的一部分):
On Error Resume Next
tVal = .GetData(label & " " & i.Value)
On Error GoTo 0
If CBool(tVal) Is Nothing Then
i.offset(0, 1).Value = "-"
Else
i.offset(0, 1).Value = .GetData(label & " " & i.Value)
End If
我正在弄乱它,但无法解决该错误。我希望这里有人可以告诉我如何解决这个问题?
答案 0 :(得分:0)
Try something like this?
Dim errorInGettingValue As Boolean
On Error Resume Next
tVal = .GetData(Label & " " & i.Value)
If Err.Number <> 0 Then errorInGettingValue = True '<~~ Check if error happened
On Error GoTo 0
If errorInGettingValue Then
i.Offset(0, 1).Value = "-"
Else
i.Offset(0, 1).Value = .GetData(Label & " " & i.Value)
End If