I need help figuring out why I get a sharing violation error. I have three excel files, call them A,B, and C.
File A copies Data into File B and closes the file, File C pulls data from a query from File B. This is done manually through the click of a button.
When both File A and File C are open, and I try to perform the macro that opens File B replaces values, closes and saves it, I get a sharing violation error. Can anyone help me out?
Code:
File C connects through a network path to File B for it's query connection.
File A copies Data into File B and closes the file, File C pulls data from a query from File B. This is done manually through the click of a button.
When both File A and File C are open, and I try to perform the macro that opens File B replaces values, closes and saves it, I get a sharing violation error. Can anyone help me out?
Code:
VBA Code:
Sub addData()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Application.ScreenUpdating = False
'Application.DisplayAlerts = False
Workbooks.Open "C:\Users\Owner\Documents\Automation\OrderData.xlsx"
Set wsCopy = Workbooks("Order_Entry.xlsm").Worksheets("Orders")
Set wsDest = Workbooks("OrderData.xlsx").Worksheets("Orders")
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
'3. Clear contents of existing data range
wsDest.Range("A4:P" & lDestLastRow).Clear
'4. Copy & Paste Data
wsCopy.Range("A4:P" & lCopyLastRow).Copy _
wsDest.Range("A4")
Workbooks("OrderData.xlsx").Close SaveChanges:=True
'Application.DisplayAlerts = True
End Sub
File C connects through a network path to File B for it's query connection.