Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Excel 2010: Default actions and message boxes depending on whether file is in use.

Posted on 2016-07-25
12
62 Views
Last Modified: 2016-08-10
I would like to use VBA code to do the following when a spreadsheet is opened:
  1. If the file is already in use by someone else, have a messagebox pop up and say something like" User nnnnn is using this file right now, and you must wait until it is not in use to enter your information."  Then when the user clicks "OK" Excel would close.  The user WOULD NOT get the normal message about the file being in use and asking whether or not they want to open it read-only or be notified when it is available.
  2. If the file is NOT already in use, just have a messagebox that says to make the desired data entry and save and close the file, as others need to input information also.

Can this be done?
0
Comment
Question by:Kevin
  • 5
  • 4
  • 3
12 Comments
 
LVL 18

Accepted Solution

by:
Roy_Cox earned 500 total points
ID: 41728858
Hi try this

Option Explicit

Private Sub Workbook_Open()
    Application.DisplayAlerts = False

    If ThisWorkbook.MultiUserEditing = False Then
        MsgBox "You may continue to add data, but please close when finished. Others need to use it.", vbInformation, "File not shared"
    Else
        MsgBox "This workbook is already in use. ", vbCritical, "Closing"
        ThisWorkbook.Close False
    End If
    Application.DisplayAlerts = True
End Sub

Open in new window

0
 

Author Comment

by:Kevin
ID: 41729321
Thank you for the help.  The first part worked fine and gave the "please close when finished" message.  But when I opened the file on a second computer, I still get the standard dialog box

 Excel locked file message
which still allows the second user to open the file.  We want to prevent two people from being in the file at the same time.

Any ideas whether or not the standard Excel dialog box above can be bypassed?
0
 
LVL 30

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41729436
What about this?

Private Sub Workbook_Open()
Application.DisplayAlerts = False
If ThisWorkbook.ReadOnly Then
   MsgBox "The file is already open. Try again later...", vbInformation
   ThisWorkbook.Close False
End If
Application.DisplayAlerts = True
End Sub

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Kevin
ID: 41729450
Neeraj,

That doesn't work either.  It seems like maybe the DisplayAlerts flag only works after the file is opened.  And that happens after the default opening Excel message...

File already open
0
 
LVL 30

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41729464
I tested the code and it works at my end.

Make sure Application.EnableEvents is set to True
In Immediate window type ?Application.EnableEvents and press Enter, you should get True

Edit: I think you already told that the first part worked for you that means Application.EnableEvents is set to True otherwise the workbook open event wouldn't have been triggered.
0
 

Author Comment

by:Kevin
ID: 41729496
Enable Events is true.  I am testing with two PCs on my network.  I open the file and get the normal message that says to quickly enter data and close.

While the file is open, I use another PC on the network to open the file.  But I still get the default message about opening the file that is use by another user BEFORE it opens and I get my VBA code to execute.

I am not sure why it is working for you but not for me...
0
 
LVL 30

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41729507
Yeah that is surprising me as well.
0
 
LVL 18

Assisted Solution

by:Roy_Cox
Roy_Cox earned 500 total points
ID: 41729841
Sharing workbooks is always problematic and should be avoided. I think the reason the code does not run when you open the workbook as second user  is because as far Excel is concerned that event has already run.

This code will show who is using the workbook whilst shared.
Option Explicit

Private Sub Workbook_Open()
    Application.DisplayAlerts = False
    If ThisWorkbook.MultiUserEditing = False Then
        MsgBox "You may continue to add data, but please close when finished. Others need to use it.", vbInformation, "File not shared"
    Else
        MsgBox "This workbook is already in use. ", vbCritical, "Closing"
        Application.Dialogs(xlDialogFileSharing).Show
        ThisWorkbook.Close False
    End If
    Application.DisplayAlerts = True
End Sub

Open in new window


Also, this code can be useful to close a workbook if a user leaves it open.First of all set a time limit eg 10 minutes .
Private Sub Workbook_Open() 
    EndTime = Now + TimeValue("00:10:00") 
    RunTime 
End Sub 

Open in new window


This resets the timer if a change in the workbook occurs
 
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If EndTime Then
        Application.OnTime _
        EarliestTime:=EndTime, _
        Procedure:="CloseWB", _
        Schedule:=False
        EndTime = Empty
    End If
    EndTime = Now + TimeValue("00:10:00")
    RunTime
End Sub

Open in new window


If no changes have occurred then this runs
Public EndTime 
Sub RunTime() 
    Application.OnTime _ 
    EarliestTime:=EndTime, _ 
    Procedure:="CloseWB", _ 
    Schedule:=True 
End Sub 
 
Sub CloseWB() 
    Application.DisplayAlerts = False 
    With ThisWorkbook 
        .Saved = True 
        .Close 
    End With 
End Sub 

Open in new window

0
 

Author Comment

by:Kevin
ID: 41730142
Thanks Roy. I just recently saw how to allow "Share Workbook" from the Review tab on the ribbon.  So I set it to share that way so that they macro could tell who else had the file open, even if it was to only close the file afterwards.  The problem with that, is that even if I am the First and Only person to open the file that way, it thinks that it is in a "shared state" (i.e. open somewhere else) and will then close...
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41730595
As I said sharing files is a pain and should be avoided. Is there no other way you can collect the data?
0
 

Author Closing Comment

by:Kevin
ID: 41750867
Thank you both.  The first solution actually seems to give me what I need and the second was extra helpful as well.
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41751500
Pleased to help
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question