Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Excel Workbook on Shared Drive-Use Command Button to Close

0

I created an Excel workbook, housed on a network drive, that is accessable by three people involved in entering data - myself and 2 co-workers.  The workbook contains three (3) worksheets - database-which is very hidden, a support worksheet containing tables-used to lookup values on the userform-which is hidden, and a "home" worksheet - the first sheet one sees when the workbook is opened.  The userform is launched from the home worksheet via a command button.  Once the user has finished their entry, there is a "save" command button on the userform. The support worksheet can be viewed by also clicking a command button on the "home" worksheet.  I've added a "timer" so that the worksheet will automatically close after 5 minutes of inactivity (based on the first person opening the workbook). 

Sub SetTimer()
    DownTime = Now + TimeValue("00:05:00")
    Application.OnTime EarliestTime:=DownTime, _
      Procedure:="ShutDown", Schedule:=True
End Sub
Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=DownTime, _
      Procedure:="ShutDown", Schedule:=False
 End Sub
  Sub ShutDown()
    Application.DisplayAlerts = False
    With ThisWorkbook
        .Saved = True
        .Close
    End With
End Sub

After the elapsed time, the support tab will be hidden and the file will close.  After a lot of testing, and learning VBA along the way, everything works fine. 

Private Sub Workbook_BeforeClose(Cancel As Boolean)

     Worksheets("Home").Visible = True
    Worksheets("Support").Visible = False 
   'must save, if not save, it is not in effect.   
    Application.DisplayAlerts = True
    Call StopTimer 
    Me.Save 

End Sub

My dilemma:  When one person has the workbook open, and a second person opens the workbook, Excel lets the second person know the workbook is already open.  Unfortunately, when the second instance of the workbook is opened, and the user will make an entry (instead of waiting until the file is closed), the user will "save as" and close the file - creating a duplicate file which requires manual merging of new data into the original file.

I have added a command button to the "home" worksheet, with a macro designed to exit but not save the file (for the situation described above).  The problem is that, instead of using the Exit button, the second user will click the "X" on the worksheet to close the file, which again brings up the message to "save", "save as", or "cancel".

How can I (1) supress the "X" on the "home" worksheet, forcing a second user to use the Exit button without saving, or saving as, the file, and (2) allow the timer to continue to run so that the file will automatically close after the elapsed period of inactivity when the first person opened the file?  Searched numerous forums and sites, and found code to suppress the "X" on the "home" worksheet, but the timer does not continue to run.  

I'm not certain if both of the above can be accomplished, but would welcome any assistance, direction as to what I'm missing.

Answer
Discuss

Answers

0
Selected Answer

Your timer attempts (but ultimately fails) to solve a problem that you shouldn't have. Therefore the solution is in not having the problem rather than fixing the solution that can't be made to work whatever. That's the theory.

In practice there are solutions for multiple access needs and they burn down to allowing access for small fractions of seconds, not minutes, or deny access to all but the first comer. Presuming that you don't want the latter, the theory is that you program your access in such a way that your application won't crash if, as chance will have it, access is demanded during that small fraction of a second while another user is having the file open. Instead, you want the code to wait until the other user has finished reading or writing.

With the above in mind, you need to allow access to your workbook to no one. Instead, everybody gets a copy of the user form and that form controls access to the workbook on the network. It reads from that workbook, within a small fraction of a second, when it needs and writes, in another small fraction of a second when it's done.

Discuss

Discussion

Minnie. I think the approach suggested by Variatus is the way to go. I guess that if a very recent update from another user might affect the input made by the user, you could get the save time when the user form was launched by that user and, on submission, check again to be sure that network file hasn't been saved since then.
John_Ru (rep: 6152) May 18, '21 at 6:31 am
Add to Discussion
0

Thanks for the spirited response and insight.

My organization requires data be kept, and maintained, on a calendar year basis - thus, the Excel workbook approach.

Three specific departments within the organization jointly administer this program, and are responsible for the accuracy of the reports generated, which is solely based on the accuracy of the data entered in the workbook.

The question originally posed was to try to get one step closer, after having successfully implemented the upgraded workbook with userform last year, to ease of use and increase efficiency for all who must enter data - approximately 250,000 keystrokes (minimum) per record - and to have sufficient time to enter the data correctly the first time around.

Again, thanks for taking a look at the question posed.

Discuss

Discussion

This sounds pretty much like standard fare. Just split your existing workbook in two. One to have the user form, the other having the data. The user opens the WB with the form, enters the data, and when he/she presses OK the other workbook is opened, data written, and closed again - all in a split second.
If the workbook with the data is in use at that moment the program has a loop to keep trying for 3 or 5 seconds ( like they do when opening a web site programmatically). Of course, the WB will never be busy for 3 seconds because to write or read one dataset takes less than one tenth of a second.
Point is, you have most of this already. All you need to do is to split your workbook. Then you give a copy of the user form to each user and they all write data to the WB on the network drive. To read from there works the same way, jusdt in the opposite direction.
Variatus (rep: 4889) May 18, '21 at 9:16 pm
Again, I want to thank you for reviewing the question submitted.

I will review your recommendation, and discuss with my IT support person, to determine if/how the back-end database can (1) be hidden so no one can accidentally access it directly or delete it, and (2) so that a new database for each calendar year can be created (as required) and "linked" to the front-end userforms.
Minnie (rep: 2) May 25, '21 at 8:41 pm
Hi Minnie,
You don't need to disable the current system while developing the improvement. It's just another way of writing to the db. The risks of accidental destruction or deletion wouldn't change. If anything they would be diminished because fewer people would access the file routinely.
Treat introduction of some safety measures as a different subject. Again, if anything, the task would become easier if there is no need to enable manual access by authorised staff. However, Excel isn't designed to be burglar-proof. The only guard against malicious intent is a rigorous backup system.
I'm sorry I didn't help you with your original intention. I promise to do better if you change the direction.
Variatus (rep: 4889) May 26, '21 at 3:05 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login