Excel 我试图在数据透视表中搜索数据并在msgbox中显示

Excel 我试图在数据透视表中搜索数据并在msgbox中显示,excel,vba,pivot-table,Excel,Vba,Pivot Table,因此,我有一个会计表,每天都要在其中输入姓名。我试图写一些VBA搜索这些名字,一个接一个后,他们在另一个表,这是一个未付款的付款表键入。该表中有所有未付款的日期列表,以及相同数据的数据透视表。我现在能够提取未付款,但当搜索的名称不在未付款表中时,我遇到了一个错误。我是VBA的新手,到目前为止,我所写的全部内容都是通过查看帮助、论坛、复制粘贴代码和一些过期版本 应用程序定义或对象定义错误-1004 尝试替换这部分代码: search_value = ActiveSheet.PivotTables(

因此,我有一个会计表,每天都要在其中输入姓名。我试图写一些VBA搜索这些名字,一个接一个后,他们在另一个表,这是一个未付款的付款表键入。该表中有所有未付款的日期列表,以及相同数据的数据透视表。我现在能够提取未付款,但当搜索的名称不在未付款表中时,我遇到了一个错误。我是VBA的新手,到目前为止,我所写的全部内容都是通过查看帮助、论坛、复制粘贴代码和一些过期版本

应用程序定义或对象定义错误-1004


尝试替换这部分代码:

search_value = ActiveSheet.PivotTables("PivotTableOutstandings").GetPivotData("Amount", "Customer", Target)
MsgBox search_value
使用此选项编辑

Dim search_value As Range
' try to assign to variable
On Error Resume Next
' in case there is nothing - there will be an error
Set search_value = ActiveSheet.PivotTables("PivotTableOutstandings").GetPivotData("Amount", "Customer", Target)
On Error GoTo 0

' if value found - it is assigned to variable
' error comes in the next line. Object required error
If Not search_value Is Nothing Then
    ' show value
    MsgBox search_value
End If
看看它是如何工作的

这是应用程序定义或对象定义的错误-1004。这一行是-search_value=ActiveSheet.PivotTables(“PivotTableOutstandings”).GetPivotData(“Amount”、“Customer”、“Target.value”)您是否尝试将
.GetPivotData(“Amount”、“Customer”、Target.value)”与
Target.value
一起使用,而不仅仅是
Target
?查找正确的值时不会出现问题。当搜索词不存在时发生。例如,如果未偿清单上有人A的名字,我搜索了人A,我就得到了他的未偿金额。但是,如果我搜索的是B,而他的名字不在列表中,那么我会得到1004个错误。如果不是ActiveSheet.PivotTables(“PivotTableOutstandings”)。GetPivotData(“Amount”,“Customer”,Target.Value)什么都不是,那么搜索值=ActiveSheet.PivotTables(“PivotTableOutstandings”)。GetPivotData(“Amount”,“Customer”,Target.Value).Value
不,不起作用。我在msgbox搜索值行上方添加了该行,以取代错误行“search_value=ActiveSheet.PivotTables(“PivotTableOutstandings”).GetPivotData(“Amount”、“Customer”、Target)”。我还是犯了错误。错误1004在此处设置错误。运行时错误424-需要对象。我正在编辑出错的地方。它是在一行-如果不是搜索值是什么then@SahilDoshi检查最新的编辑-您必须将
Dim search\u值声明为Range
,因为
.GetPivotData
方法。工作起来很有魅力!谢谢!
Dim search_value As Range
' try to assign to variable
On Error Resume Next
' in case there is nothing - there will be an error
Set search_value = ActiveSheet.PivotTables("PivotTableOutstandings").GetPivotData("Amount", "Customer", Target)
On Error GoTo 0

' if value found - it is assigned to variable
' error comes in the next line. Object required error
If Not search_value Is Nothing Then
    ' show value
    MsgBox search_value
End If