简体   繁体   中英

Excel VBA writes data to second workbook, but starts opening read-only versions because " _ is already open

I have some VBA script in one Excel Workbook that has three subs that each either read from a second Workbook. Each of the subs uses the following algorithm (simplified to distill the interaction with the second book):

Public Sub EditRemote()
Dim remoteDataSheet As Worksheet
Dim source As String        'Source worksheet name
Dim target As String        'Target worksheet name
Dim path As String
Dim wkbName As String

    source = "CountData"     
    path = ThisWorkbook.Worksheets("Parameters").Range("B2").Value  
    wkbName = ThisWorkbook.Worksheets("Parameters").Range("A2").Value
    target = "CountData"

    Application.EnableCancelKey = xlDisabled 


    Set localDataSheet = ThisWorkbook.Sheets(source)
    If Not WorkbookIsOpen(wkbName) Then
        Workbooks.Open (path)
    End If
    Set remoteDataSheet = Workbooks(wkbName).Sheets(source)

    remoteDataSheet.Cells(1,1) = localDataSheet.Cells(1,1)
    remoteDataSheet.Cells(1,2) = localDataSheet.Cells(1,2)

    Workbooks(wkbName).Close SaveChanges:=True

End Sub

Function WorkbookIsOpen(targetWorkbook As String) As Boolean
    Dim testBook As Workbook

    On Error Resume Next
    Set testBook = Workbooks(targetWorkbook)
    If Err.Number = 0 Then
        WorkbookIsOpen = True
    Else:
        WorkbookIsOpen = False
    End If
End Function

There is also a pivot table in this Workbook that draws its data from the second file though an external data connection as well. The issue that is plaguing me is that it seems that not initially but after a few operations, these subs stop making the edits properly and instead it opens a read only copy of the second Workbook. When I try to open the second workbook manually I get a message saying that the file is already open and is locked for editing. Right now both files are local to my computer and couldn't be opened by anyone else. What am I missing to be sure that I can make the code work as intended?

I made some modification to your code, ran it a few times, and didn't get your "Read-only" message.

In your code the line of declaring localDataSheet is missing, added Dim localDataSheet As Worksheet , also added Dim remoteWb As Workbook for the remote workbook.

(didn't modify your Funtion WorkbookIsOpen code).

Sub EditRemote Code

Option Explicit

Public Sub EditRemote()

Dim remoteDataSheet As Worksheet
Dim localDataSheet As Worksheet
Dim source As String        'Source worksheet name
Dim target As String        'Target worksheet name
Dim path As String
Dim wkbName As String
Dim remoteWb As Workbook

    source = "CountData"
    path = ThisWorkbook.Worksheets("Parameters").Range("B2").Value
    wkbName = ThisWorkbook.Worksheets("Parameters").Range("A2").Value
    target = "CountData"

    Application.EnableCancelKey = xlDisabled

    Set localDataSheet = ThisWorkbook.Sheets(source)
    ' check if workbbok already open
    If Not WorkbookIsOpen(wkbName) Then
        Set remoteWb = Workbooks.Open(path)
    Else
        Set remoteWb = Workbooks(wkbName) ' workbook is open >> set remoteWb accordingly
    End If

    Set remoteDataSheet = remoteWb.Sheets(source)

    remoteDataSheet.Cells(1, 1) = localDataSheet.Cells(1, 1)
    remoteDataSheet.Cells(1, 2) = localDataSheet.Cells(1, 2)

    Workbooks(wkbName).Close SaveChanges:=True

End Sub

Just to verify the data in your Excel "Parameters" sheet, the screen-shot below shows the data I used for my testing.

Cell A2 contains the "Clean" workbook name.

Cell B2 contains workbbok "full" name - path + "clean" workbook name.

在此输入图像描述

After some further testing to diagnose the issue, I found that there was nothing wrong with the VBA code, but rather the external data connection to the remote Workbook was locking that Workbook every time I refreshed the data in the pivot table that used the external data connection as its source. It isn't unlocking the file when it is done refreshing, and that leaves the file locked until I close the Workbook with the pivot table. Now I just need to solve that problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM