Share via

Very stuck on VBA sub error #1004

Anonymous
2018-05-21T15:20:06+00:00

Hi all,

First time poster here. I am currently writing a subroutine in VBA for my excel workbook to achieve two things, (1) when a specific cell is edited update the value of a pivottable filter, and (2) refresh all powerquery tables in the workbook. It is also worth noting that I am not super strong in VBA and have mostly cobbled this code together from snippets I've found around the web. Below is the code in full:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    'This line stops the worksheet updating on every change, it only updates when cell

    'D3 is touched

    If Intersect(Target, Range("D3")) Is Nothing Then Exit Sub

    On Error GoTo errorhandler

    'Set the Variables to be used

    Dim qt As QueryTable

    Dim lo As ListObject

    Dim pt As PivotTable

    Dim Field As PivotField

    Dim NewCat As String

    'Here you amend to suit your data

    Set pt = Worksheets("Summary Dashboard").PivotTables("PivotTable1")

    Set Field = pt.PivotFields("Participation Agreement No")

    NewCat = Worksheets("Summary Dashboard").Range("D3").Value

    'This updates and refreshes the PIVOT table

    With pt

        Field.ClearAllFilters

        Field.CurrentPage = NewCat

        pt.RefreshTable

    End With

    'updating and refreshing queries

    For Each qt In Worksheets("Helper Sheet").QueryTables

        qt.Refresh BackgroundQuery:=False

    Next qt

    For Each lo In Worksheets("Helper Sheet").ListObjects

        lo.QueryTable.Refresh BackgroundQuery:=False

    Next lo

    Set qt = Nothing

errorhandler:

    If Err.Number <> 0 Then

    Msg = "Error # " & Str(Err.Number) & " was generated by " _

         & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description

    MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext

    MsgBox ("Please enter a PA Number that exists in all datasets.")

    Exit Sub

    End If

End Sub

Now when I edit the value in D3 and hit enter, the values do not update and the query tables do not refresh, but on navigating back to the cell (either by clicking or using the arrow keys) the values do update (however the query tables still do not refresh), but the following error is produced:

Error # 1004 was generated by VBAProject

Error Line: 0

Application-defined or object-defined error

I have done quite a bit of googling to no avail and I think my newness to VBA is keeping me from properly debugging this. Please feel free to ask questions or for clarification, any help is appreciated! Thanks.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2018-05-21T15:54:52+00:00

    Do you have those two worksheets with the names of "Summary Dashboard" and "Helper Sheet" in your workbook, with exactly the same spelling and no leading or trailing spaces?

    Was this answer helpful?

    0 comments No comments