1
votes

I've encountered a strange thing: I've joined three workbooks: Personal Data Tracker, Global Tracker and the workbook with pivots and charts. The logic is as it follows: the user clicks on a button after the work is finished so the data is copied to the GL Tracker. Once the change event is triggered in the GL Tracker Table, the last workbook opens, the pivot is refreshed upon the open vent and the wb is closed.

Everything seems to be working fine, however when I run the macro live, at the very end I get an error message about

"Application-defined or object-defined error".

Only OK and Help button displayed, it doesn't make the VBE Open so I could debug it. Would anyone know what it may be happening even if the whole chain works fine? Thank you.

Code from the Personal Tracker:

Sub test()

Dim path As String
Dim wb As Workbook

path = ThisWorkbook.path & "\Dest.xlsm"

Application.ScreenUpdating = False

ThisWorkbook.Sheets("Total").Range("R1").Value = Date
Range("R1").Font.Color = VBA.ColorConstants.vbWhite
Worksheets("TOTAL").Range("B2:B13").Copy

On Error GoTo Handler
Workbooks.Open (path)
On Error GoTo 0

Set wb = Workbooks("Dest")
Worksheets("Sheet1").Range("B2").PasteSpecial Paste:=xlPasteValues
Exit Sub

Handler:
 MsgBox "Someone else is saving their data at the moment." & vbNewLine & _
    "Please try in a few seconds"
End Sub

Code from the GL Tracker:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Dim MRange As Range
Dim wbPivot As Workbook
Dim pt As PivotTable
Dim ws As Worksheet
Dim Name As String
Dim answer As VbMsgBoxResult

Set MRange = ThisWorkbook.Sheets(1).Range("Table1")
Name = Application.UserName

Application.ScreenUpdating = False

If Not Intersect(Target, MRange) Is Nothing Then
    Application.EnableEvents = True
Set wbPivot = Workbooks.Open("C:\Users\jakub\Desktop\Excel - various\Pivot.xlsm")
End If

'refresh

For Each ws In wbPivot.Worksheets
    For Each pt In ws.PivotTables
        pt.PivotCache.Refresh
        pt.Update
        pt.RefreshTable
    Next
Next

'saving

Application.ScreenUpdating = True

If Application.UserName <> "Jakub Tracz" Then
    MsgBox "User not authorised. Workbook will be closed."
            wbPivot.Close True
            ThisWorkbook.Close True
    Else
        answer = MsgBox(Prompt:="Do you want to save and close the workbook?", _
            Buttons:=vbYesNo + vbQuestion)

    Select Case answer
        Case vbYes
            wbPivot.Close True
            ThisWorkbook.Close True
        Case vbNo
            MsgBox "Welcome, " & Application.UserName
    End Select
End If
End Sub
1
Please include the code you are using in your question. - braX
@braX It's added. - JakubTracz
Since you say no line is highlighted when you get the error, you will want to set a breakpoint (F9) on a line close to (before) the point where you think the error is occurring. You can then use F8 to step through it line by line until you get the error. Once you isolate the line with the error, include that in your question as well. - braX
Edit: I forgot to add that this message occurs only when I choose to save and close the pivot wb in the last procedure. I'm looking closer at it right now. - JakubTracz
I've just checked the scenario when I'm not allowed to see it - and I got the same error. Apparently, it's about closing it immediately in the same procedure. I'll try to assign it to change event and see what happens. - JakubTracz

1 Answers

0
votes

I'm going to give you a proof of concept code as an example for you to use. This will not exactly answer your question with code you can just copy/paste, but you will be able to use this to put it together the way you want it to work instead of me making assumptions about many things and restructuring it myself.

This simply demonstrates how to use a workbook object variable in one routine that can reference another workbook, and how to make changes to that 2nd workbook and save/close it.

Sub Tracker_Update()
  Dim wbPivot as Workbook

  ' open the workbook
  Set wbPivot = Workbooks.Open("C:\Users\jakub\Desktop\Excel - various\Test.xlsx")

  ' optionally make it hidden
  wbPivot.Visible = False

  With wbPivot

    ' pretend this code updates the pivot table
    .Worksheets(1).Range("A1") = "hello world"

    ' Close and save it
    .Close True

  End With

  ' optionally clear the variable 
  ' this is not really needed in VBA, but if you eventually
  ' start using VB.NET with Excel as a COM object,
  ' you will want to know how to do this part when you are done
  Set wbPivot = Nothing

End Sub

I think you will like this approach in the end much better in the end anyway, as the code isn't scattered around so much in different places. Easier to debug later, and easier for someone else to understand what you are doing if and when you leave the company.