Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared Excel Document

Tags:

excel

vba

I've got a Excel documents with macros with input and update forms for information, the document is used as a monthly database where you can run reports from as well.

The issue is that when using the document sometimes the input and update options are used at the same time causing information loss. Both the input and output save at the end of the macro to minimise the losses, but I was wondering if there is anyway of checking at runtime if there is a macro being use by another user and if so delay the next macro run until the other user is finished?

like image 796
Sara C Avatar asked Aug 05 '16 14:08

Sara C


People also ask

Can multiple users edit an Excel file at the same time?

You and your colleagues can open and work on the same Excel workbook. This is called co-authoring. When you co-author, you can see each other's changes quickly—in a matter of seconds.

Can I share an Excel file with multiple users?

Click Review > Share Workbook. On the Editing tab, select the Allow changes by more than one user ... check box. On the Advanced tab, select the options that you want to use for tracking and updating changes, and then click OK.

What is a shared workbook in Excel?

By sharing an Excel file, you are giving other users access to the same document and allow them to make edits simultaneously, which saves you the trouble of keeping track of multiple versions.


1 Answers

There is one way I can think of. Logically it should work. However I have not tested it.

  1. Create a temp sheet and hide it
  2. When anyone runs a macro, check if cell A1 of that sheet is empty or not
  3. If it is empty then run the macro
  4. Before running the macro, write to that cell and once the macro is run, clear the contents of the other cell
  5. Sandwich your macro code as mentioned below

Code

Sub Sample()
    Dim ws As Worksheet

    ThisWorkbook.Save
    Doevents

    Set ws = ThisWorkbook.Sheets("HiddenSheetName")

    If Len(Trim(ws.Range("A1").Value)) = 0 Then
        ws.Range("A1").Value = "Macro Starts"
        ThisWorkbook.Save
        Doevents

        '
        '~~> Rest of your code goes here
        '

        ws.Range("A1").ClearContents
        ThisWorkbook.Save
        Doevents
    Else
        MsgBox "Please try after some time. There is a macro running... Blah Blah"
    End If
End Sub

CAUTION: Once the code runs, you cannot undo the changes since the code save the file programatically. The changes are permanent. In a file which is not shared, you can undo by closing the file without saving and re-opening it.

like image 55
Siddharth Rout Avatar answered Oct 28 '22 10:10

Siddharth Rout