EDIT I forgot to mention that the error occurs on the .activate
line.
I have a simple macro set up that updates a separate document with some data. I want the the macro to automatically activate when the user saves but I am getting this error "Object variable or With block variable not set" whenever I try and use the macro.
I have the code in the ThisWorkbook
module. I have put the code in a new module and it works fine with no errors. The error only occurs when the code is in the ThisWorkbook
module.
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim wbMe, wbOut As Workbook
Application.ScreenUpdating = False
Set wbMe = ActiveWorkbook
'Sets the destination for the data as well as automatically sending the data to the sheet that corresponds with the date inputted.
Set wbOut = Workbooks.Open("/Users/MathieuKlein/Desktop/ValveStockMaster.xlsx")
'This section deals with the actual process of copying the data and pasting it to the other excel file.
With wbOut
.Activate
' The following sets up the total of valves
wbOut.Sheets("Stock").Range("D2:H20") = wbMe.Sheets("Macro Data").Range("K104:O122").Value
wbOut.Sheets("Stock").Range("D22:H37") = wbMe.Sheets("Macro Data").Range("K123:O138").Value
wbOut.Sheets("Stock").Range("D39:H46") = wbMe.Sheets("Macro Data").Range("K139:O146").Value
wbOut.Sheets("Stock").Range("D48:H50") = wbMe.Sheets("Macro Data").Range("K147:O149").Value
wbOut.Sheets("Stock").Range("D52:H53") = wbMe.Sheets("Macro Data").Range("K150:O151").Value
wbOut.Sheets("Stock").Range("D55:H58") = wbMe.Sheets("Macro Data").Range("K152:O155").Value
wbOut.Sheets("Stock").Range("D61:H61") = wbMe.Sheets("Macro Data").Range("H14,H3,H5,H7,H9").Value
End With
With wbOut
.Save
.Close
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
I expect the macro to activate when the user saves the document but this error pops up preventing that.