Cannot save file due to sharing violation - Query connected to other workbook

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
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:

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.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top