【问题标题】:How fix object required error in vba in with statement?如何在 with 语句中修复 vba 中所需的对象错误?
【发布时间】:2019-05-02 16:18:49
【问题描述】:

我想创建一个按钮,当用户按下它时,它将执行以下操作:

  1. 在 Eff 工作表上的数据透视表中按下按钮用户标记名称之前
  2. 按下按钮后,相同名称将在工作表 Pivot_All 上的另一个数据透视表中进行过滤

我试过这个,但在 .PivotItems(a(g)).Visible = True 它给了我对象需要的错误

Public Function GetLength(a As Variant) As Integer
   If IsEmpty(a) Then
      GetLength = 0
   Else
      GetLength = UBound(a) - LBound(a) + 1
End If
 GL = GetLength
End Function

Public Function a(ByVal rng As Range) As Variant
    Dim f As Long, r As Range
    ReDim arr(1 To rng.Count)

    f = 1
    For Each r In rng.Cells
        arr(f) = r.Value
        f = f + 1
     Next r

  a = arr
End Function

Sub Macro6()
 Dim rngCopy As Range
 Dim rngPaste As Range
 Dim rng As Range
 Set rng = Selection

  Sheets("Pivot_All").Activate

  ActiveSheet.PivotTables("PivotTable1").PivotFields("Empl").ClearAllFilters
  With ActiveSheet.PivotTables("PivotTable1").PivotFields("Empl")
    For i = 1 To .PivotItems.Count - 1
        .PivotItems(.PivotItems(i).Name).Visible = False
    Next i
 End With

With ActiveSheet.PivotTables("PivotTable1").PivotFields("Empl")
      For g = 0 To GL
        .PivotItems(a(g)).Visible = True
      Next g
End With

End Sub

【问题讨论】:

  • a 正在等待范围 a(ByVal rng As Range) 但您提交了一个数字 g 作为参数。您必须提交 Range 对象。
  • 你的意思是像For Each cell In a .PivotItems(a(cell)).Visible = True Next cell这样的东西吗?
  • 在下面查看我编辑的答案。

标签: excel vba pivot-table


【解决方案1】:

a 正在等待范围 a(ByVal rng As Range),但您提交了一个数字 g 作为参数。您必须提交 Range 对象。

另外GL 未在您的程序中定义,我假设您想改用GetLength

所以应该是这样的:

Dim ResultOfA As Variant
ResultOfA = a(Range("your range here"))

For g = 0 To GetLength(ResultOfA) 
    .PivotItems(ResultOfA(g)).Visible = True
Next g

我建议始终激活 Option Explicit:在 VBA 编辑器中转到 ToolsOptionsRequire Variable Declaration


请注意,您可以在一行中将连续范围读入数组:

Dim ArrValues() As Variant
ArrValues = Range("A1:A10").Value

Debug.Print ArrValues(1, 5) '= A5

如果你使用它,你可能不再需要你的函数a


你最终会得到类似的东西:

Option Explicit

Public Sub Macro6()
    Dim rngCopy As Range
    Dim rngPaste As Range
    Dim rng As Range
    Set rng = Selection

    Dim ws As Worksheet
    Set ws = Worksheets("Pivot_All")

    With ws.PivotTables("PivotTable1").PivotFields("Empl")
        .ClearAllFilters

        Dim i As Long
        For i = 1 To .PivotItems.Count - 1
            .PivotItems(.PivotItems(i).Name).Visible = False
        Next i

        Dim ResultOfA As Variant
        ResultOfA = a(rng)

        If Not IsEmpty(ResultOfA) Then
            Dim g As Long
            For g = LBound(ResultOfA) To UBound(ResultOfA)
                .PivotItems(ResultOfA(g)).Visible = True
            Next g
        End If
    End With
End Sub

Public Function a(ByVal rng As Range) As Variant
    Dim f As Long, r As Range
    ReDim arr(1 To rng.Count)

    f = 1
    For Each r In rng.Cells
        arr(f) = r.Value
        f = f + 1
    Next r

    a = arr
End Function

【讨论】:

  • GLGetLength 函数中定义为GL = GetLength。如果它是公共变量,它将起作用。
  • @shrivallabha.redij 如果它是一个返回不应该写入全局变量的值的函数(那么它应该是一个过程),这根本没有任何意义。我建议使用Option Explicit 从函数中删除GL = GetLength 行。
  • @Pᴇʜ 我没有从编码实践的角度发表评论,只是为了表明它是在 OP 的代码中定义的。我个人对发布的代码的编码方面并不满意,例如定义一个刚刚命名为 a 的函数,但这不是问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 2018-12-06
  • 2020-10-30
相关资源
最近更新 更多