VBA If workbook already open then message box & exit sub

Roggy_Swiss

New Member
Joined
May 30, 2013
Messages
10
Hi,
I have a code taking data of a one workbook (Workbook1) and putting the specified data into another workbook ("MasterFile" {Workbook2}). The code works fine if the MasterFile is NOT open, because the code works on the workbooks.open event.
What I would like is, when the MasterFile is ALREADY open, then populate a MsgBox saying something like "Workbook currently in use, please contact HR and try again later" AND exit the whole sub and return to the original data sheet (Workbook1). Please also note that the Workbook2 is password protected. I know this has probably been asked before, but I cannot find an answer corresponding to this specific issue.

Here is what I have now (not all of it, but the relevant code):

Private Sub CommandButton1_Click()

Dim MasterFile As Workbook
Set MasterFile = Workbooks.Open("G:\Time Tracking\FileName.xlsx", Password:="XY")

Worksheets("MasterFileHours").Select
Worksheets("MasterFileHours").Range("A1").Select
RowCount = Worksheets("MasterFileHours").Range("A1").CurrentRegion.Rows.Count

Application.ScreenUpdating = False
Application.Visible = False

With Worksheets("MasterFileHours").Range("A1")
.Offset(RowCount, 0).Value = WeekNo
.Offset(RowCount, 1).Value = xDate
.Offset(RowCount, 2).Value = EOWEEK
.Offset(RowCount, 3).Value = Employee
.Offset(RowCount, 4).Value = HoursEffective
.Offset(RowCount, 5).Value = HoursTarget
.Offset(RowCount, 6).Value = OverUnder
.Offset(RowCount, 7).Value = Pot60Increase
.Offset(RowCount, 8).Value = Pot60Decrease
End With

MasterFile.Save
MasterFile.Close

End Sub


THANKS IN ADVANCE!!!
BTW: A big thank you to all of you guys!! YOU ARE AMAZING! You can't believe how much impact VBA macros has in the company I work for (since I starting learning it and putting into practice, thanks to you):)
Best regards, Roger
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Code:
Option Explicit

Sub Sample()
    Dim Ret

    Ret = IsWorkBookOpen("C:\myWork.xlsx")

    If Ret = True Then
        MsgBox "File is open"
    Else
        'Place your code here!'
    End If
End Sub

Function IsWorkBookOpen(FileName As String)
    Dim ff As Long, ErrNo As Long

    On Error Resume Next
    ff = FreeFile()
    Open FileName For Input Lock Read As #ff
    Close ff
    ErrNo = Err
    On Error GoTo 0

    Select Case ErrNo
    Case 0:    IsWorkBookOpen = False
    Case 70:   IsWorkBookOpen = True
    Case Else: Error ErrNo
    End Select
End Function
 
Upvote 0
Thank you Njimack & Kemmuniemans for the quick replies.
Yes, the Worksheet2 is located on a network drive.

However, I can't quite follow your code Kemmuniemans. Where do I put my original code in your provided code?
I just tried this:

Dim Ret
Ret = IsOpen.Workbooks("G:\Time Tracking\FileName.xlsx")
If Ret = True Then
MsgBox "The MasterFile is already in use..."
Else
Set MasterFile = Workbooks.Open("G:\Time Tracking\FileName.xlsx", Password:="XY")

Worksheets("MasterFileHours").Select
Worksheets("MasterFileHours").Range("A1").Select
RowCount = Worksheets("MasterFileHours").Range("A1").CurrentRegion.Rows.Count

Application.ScreenUpdating = False
Application.Visible = False

With Worksheets("MasterFileHours").Range("A1")
.Offset(RowCount, 0).Value = WeekNo
.Offset(RowCount, 1).Value = xDate
.Offset(RowCount, 2).Value = EOWEEK
.Offset(RowCount, 3).Value = Employee
.Offset(RowCount, 4).Value = HoursEffective
.Offset(RowCount, 5).Value = HoursTarget
.Offset(RowCount, 6).Value = OverUnder
.Offset(RowCount, 7).Value = Pot60Increase
.Offset(RowCount, 8).Value = Pot60Decrease
End With

MasterFile.Save
MasterFile.Close

End If

End Sub

Note: This whole code should be in the "CommandButton1" click routine.

THANK YOU!

BR, Roger
 
Upvote 0
This should work:

Code:
Option Explicit

Private Sub CommandButton1_Click()
    Dim MasterFile As Workbook
    Dim Ret

    Ret = IsWorkBookOpen("G:\Time Tracking\FileName.xlsx")

    If Ret = True Then
        MsgBox "File is open"
    Else
Set MasterFile = Workbooks.Open("G:\Time Tracking\FileName.xlsx", Password:="XY")

 Worksheets("MasterFileHours").Select
 Worksheets("MasterFileHours").Range("A1").Select
 RowCount = Worksheets("MasterFileHours").Range("A1").CurrentRegion.Rows.Count

 Application.ScreenUpdating = False
 Application.Visible = False

 With Worksheets("MasterFileHours").Range("A1")
 .Offset(RowCount, 0).Value = WeekNo
 .Offset(RowCount, 1).Value = xDate
 .Offset(RowCount, 2).Value = EOWEEK
 .Offset(RowCount, 3).Value = Employee
 .Offset(RowCount, 4).Value = HoursEffective
 .Offset(RowCount, 5).Value = HoursTarget
 .Offset(RowCount, 6).Value = OverUnder
 .Offset(RowCount, 7).Value = Pot60Increase
 .Offset(RowCount, 8).Value = Pot60Decrease
 End With

 MasterFile.Save
 MasterFile.Close
End If
End Sub

Function IsWorkBookOpen(FileName As String)
    Dim ff As Long, ErrNo As Long

    On Error Resume Next
    ff = FreeFile()
    Open FileName For Input Lock Read As #ff
    Close ff
    ErrNo = Err
    On Error GoTo 0

    Select Case ErrNo
    Case 0:    IsWorkBookOpen = False
    Case 70:   IsWorkBookOpen = True
    Case Else: Error ErrNo
    End Select
End Function
 
Upvote 0
Hi again,
I have an interesting issue in relation to the code above.
First of all the actual function works fine. But, ANY other open workbooks kind of hide/close after the code was run, so no excel files (no matter which) show as 'open' in the Taskbar. When I open any other workbook, the other workbooks, which were "Hidden/Closed" pop up again.
You can already see this not being very user friendly. Unfortunately I have no clue why the code does this, since also i've set only the MasterFile and at the end of the code it only closes THIS MasterFile, and not ANY OTHER workbook.
Hopefully this makes sense and you're able to help/advice on this.
Thank you
BR, Roger
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,816
Members
449,049
Latest member
cybersurfer5000

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