Check if target workbook is already opened before start copying data between two workbooks

Soma88

New Member
Joined
Oct 20, 2014
Messages
3
Hi everyone,

So I have a macro which copies certain data to an other workbook. It works fine but I have this issue that every time the macro begins to run (somebody is migrating data to the main excel file) it should be checked first if the main file was opened/is open by someone else on the network to avoid confusion and data loss.

I have tried the following code:

Private Sub CommandButton1_Click()


Dim dt As String
Dim SO As String




Worksheets("migrate").Select
dt = Range("A2")
SO = Range("B2")
Ret = IsFileOpen("D:\MainTracker.xls")


If Ret = False Then
Set myData = Workbooks.Open("D:\MainTracker.xls")
Worksheets("ProjectName").Select
Worksheets("ProjectName").Range("F1").Select
With Worksheets("Projectname")
RowCount = .Range("F" & .Rows.Count).End(xlUp).Row
End With
With Worksheets("Projectname").Range("F1")
.Offset(RowCount, 0) = dt
.Offset(RowCount, 1) = SO
End With
myData.Save
Else
MsgBox "not now"
End If

End Sub

I highlighted the parts which are causing troubles.(I'm quite sure that I am not using IsFileOpen right.)

Any toughts how to solve this issue?

Thank you very much in advance!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,216,923
Messages
6,133,520
Members
449,809
Latest member
Bhad007

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