+ Reply to Thread
Results 1 to 8 of 8

Preventing activating another workbook while macro runs?

  1. #1
    Registered User
    Join Date
    01-23-2015
    Location
    Ohio, USA
    MS-Off Ver
    2010
    Posts
    27

    Preventing activating another workbook while macro runs?

    Is there a way to prevent a user from activating another excel workbook until a macro is finished running on the current one?

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,966

    Re: Preventing activating another workbook while macro runs?

    The best way to write your code is to make sure you fully specify the object you are working with.

    This will write to cell A1 on the currently active sheet, no matter what that is:

    Please Login or Register  to view this content.
    This will write to cell A1 on a specific sheet in the currently active workbook:

    Please Login or Register  to view this content.
    This will write to cell A1 on a specific sheet in a specific workbook:

    Please Login or Register  to view this content.
    And you can easily convert your code to be specific by adding

    Please Login or Register  to view this content.
    And then any range or cell call, preceed with an S:

    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    01-23-2015
    Location
    Ohio, USA
    MS-Off Ver
    2010
    Posts
    27

    Re: Preventing activating another workbook while macro runs?

    Great suggestion. But, would there be another simpler way besides changing all my code.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,966

    Re: Preventing activating another workbook while macro runs?

    Not really, unless you close all other visible workbooks with code first. Any solutions that could use macros or events are prevented from working because the macro is running, and only one macro can run at a time. Otherwise, just train your users to not do what you don't want them to do.

  5. #5
    Registered User
    Join Date
    01-23-2015
    Location
    Ohio, USA
    MS-Off Ver
    2010
    Posts
    27

    Re: Preventing activating another workbook while macro runs?

    What objects would require specifying when Workbook_Deactivate() occurs to avoid errors? Is there a list somewhere with this information that you know of? Thanks in advance for the help too!

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,966

    Re: Preventing activating another workbook while macro runs?

    My point was that you cannot use the Workbook_Deactivate event because your macro is running.

  7. #7
    Registered User
    Join Date
    01-23-2015
    Location
    Ohio, USA
    MS-Off Ver
    2010
    Posts
    27

    Re: Preventing activating another workbook while macro runs?

    Right. I meant, when using this:

    Dim W As Workbook
    Dim S As Worksheet

    Set W = Workbooks("File name.xlsm")
    Set S = W.Worksheets("Sheet1")

    S.Range("A1").Value = 3

    What objects would I need to identify specifically? Just Ranged areas? I only want to specify what I need to in my code to save time.

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,966

    Re: Preventing activating another workbook while macro runs?

    Every child object of the worksheet that can be specified must be, but that usually - for most macros - just means

    Range(Range(.....),Range(....)) becomes S.Range(S.Range(.....),S.Range(....))

    and Cells(.....) becomes S.Cells(.....)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro which opens workbook, runs macro, copies and then closes
    By Vihral in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-06-2013, 11:45 AM
  2. [SOLVED] Activating a file in a macro by referencing a cell in a workbook
    By Mrs F in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-10-2013, 04:50 AM
  3. Workbook opening when macro runs
    By Dubby20 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-14-2012, 11:58 AM
  4. open workbook macro only runs once
    By Directlinq in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-30-2009, 07:44 AM
  5. Before macro runs - check workbook name
    By Jan in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-02-2006, 09:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1