dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,375
- Office Version
- 365
- 2016
- Platform
- Windows
I have 2 buttons to copy data from the current workbook to another. One button copies and leaves the data there. The other button copies and deletes the data.
The files are on a network and if the second workbook is already open I have code that notifies the user trying to open the file and asks them to wait or contact the person who has it open.
The issue is that if the file is already open the user is asked to try again later but if they selected the button to copy the data and delete the contents, the contents will be gone and they won't be able to try again later as the data won't be there.
I have a rather lengthy sub to copy the data as there are many processes that need to be run.
This is the code for my copy and delete contents button:
and this code is from the second workbook
Part of cmdCopy calls the second workbook to open but if it is already open by someone else, how do I force it to skip the line of code?
The files are on a network and if the second workbook is already open I have code that notifies the user trying to open the file and asks them to wait or contact the person who has it open.
The issue is that if the file is already open the user is asked to try again later but if they selected the button to copy the data and delete the contents, the contents will be gone and they won't be able to try again later as the data won't be there.
I have a rather lengthy sub to copy the data as there are many processes that need to be run.
This is the code for my copy and delete contents button:
VBA Code:
Sub cmdCopyLineBlank_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Costing_tool")
ws.Unprotect
Call cmdCopy
ws.Unprotect
Call CostingDeleteAll
' ws.Protect
End Sub
and this code is from the second workbook
VBA Code:
Sub Workbook_Open()
Application.WindowState = xlMaximized
Dim file1 As Integer
Dim strLine As String
file1 = FreeFile
If Not ActiveWorkbook.ReadOnly = True Then
'only add name to the usage log if the user has it locked
Open ThisWorkbook.Path & "\usage.log" For Append As #file1
Print #file1, Environ("USERNAME") & ". Please close all the additional workbooks that will be opened " _
& " WITHOUT SAVING THEM. Then contact the user that has it open or wait until they are finished."
Close #file1
Else
'if someone else has the file open, find out who
Open ThisWorkbook.Path & "\usage.log" For Input Access Read As #file1
Do While Not EOF(file1)
Line Input #file1, strLine
Loop
Close #file1
MsgBox "The following user has the allocation sheets open: " & strLine
End If
'Worksheets("home").Protect Password:="costings"
End Sub
Part of cmdCopy calls the second workbook to open but if it is already open by someone else, how do I force it to skip the line of code?
VBA Code:
Call CostingDeleteAll