+ Reply to Thread
Results 1 to 15 of 15

Multithreading in Excel

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Multithreading in Excel

    Hi Guys,

    i am triggering code in worksheet refering to named ranges like:

    Please Login or Register  to view this content.
    users noticed that when they have opened more than 1 instance of my master workbook some macros are not triggered.
    i suppose that i am refering to application global property (application.range) and this is the root cause.

    It is possible to workaround this?

    Best,
    Jacek

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Multithreading in Excel

    That's clearly a line out of an event handler. I suspect that it's possible you are switching off event handling for longer than it needs to be. As far as I am aware, that is an Application wide setting ... as in
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Multithreading in Excel

    Thank you very much,

    interesteting.
    Generally i have in first line:

    Application.EnableEvents = False

    and after change event finishes i am swtihing it on:

    Application.EnableEvents = true

    Hmm how this can have impact on multiple workbooks?

    I was thinking...
    i have 2 the same workbooks opened.
    I am triggering worksheet change in 1st one and application doesnt know wich application.range to use because i have the same named ranges in 2nd workbook...
    This is not the cause?

    Let's see another scenario.
    When i am triggering worksheet change event i am turning off enableevents:

    The change last 1-2 seconds max. In this time it is hard to go to another workbook and click to run another macro.
    It should not have the impact? Or i am not understanding?

    Best,
    Jacek

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Multithreading in Excel

    I think it depends on how much code there is in your event handler(s). You should switch off event handling for as short a time as possible and ensure that, in the event of an error, it is switched back on before exiting the routine.

    For example, in a Worksheet Change Event handler, you only need to switch off event handling if/when you actually make change while your in the handler to avoid unnecessary execution. Bear in mind that the WCE handler will fire every time you make a change on the worksheet but you probably won't need to process every event. So, you don't need to switch off event handling and then make the decision whether or not you are going to process the change event.

    Let's say you are only interested in changes in range B11:B30. That's just 20 cells out of potentially millions that you could change. The more you limit the scope, the less opportunity there is for issues to arise.

  5. #5
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Multithreading in Excel

    Application.Range("Vn_Input_HANA_Production") will refer to that range in the active workbook. If the workbook that is being changed isn't the active one, then your event wouldn't do anything.
    Rory

  6. #6
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Multithreading in Excel

    hen your event wouldn't do anything.
    It would not throw any error? Thanks!

    Hmm so what if i would have activated 2nd workbook but run code from first one?

    I think it depends on how much code there is in your event handler(s). You should switch off event handling for as short a time as possible and ensure that, in the event of an error, it is switched back on before exiting the routine.

    For example, in a Worksheet Change Event handler, you only need to switch off event handling if/when you actually make change while your in the handler to avoid unnecessary execution. Bear in mind that the WCE handler will fire every time you make a change on the worksheet but you probably won't need to process every event. So, you don't need to switch off event handling and then make the decision whether or not you are going to process the change event.

    Let's say you are only interested in changes in range B11:B30. That's just 20 cells out of potentially millions that you could change. The more you limit the scope, the less opportunity there is for issues to arise.
    thank you!

    thank you very much for your explanation. It can be an issue. I have worksheet and only few cells should be triggered using event handler.

    So it is a good practice to do application.enableevents before each cell which can be triggered (each line of code) and after it just turn on this?
    Like if range(B30) = "Yes" then
    application.enableevents = false
    range(B20) = "1"
    application.enableevents = true
    end if
    ??

    And how to address only specific range to be triggered like B11:B30?

    Best,
    Jacek
    Last edited by jaryszek; 02-01-2021 at 10:29 AM.

  7. #7
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Multithreading in Excel

    It should throw an error (1004) but I don't know if you have error handling.

    I note you refer to the users having multiple instances of your master workbook open - is that a template, or are they using multiple instances of Excel?

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Multithreading in Excel

    So it is a good practice to do application.enableevents before each cell which can be triggered (each line of code) and after it just turn on this?
    Pretty much, yes. However, if possible, you would be better off referring to the Target cell rather than specific cells

    And how to address only specific range to be triggered like B11:B30?
    You already have the right construct:
    Please Login or Register  to view this content.
    Note that a Change Event handler, if in place, will fire for every change. It's more a case of limiting how much work it needs to do when it is fired.

    This is the solution to a recent thread. Notice that quite a few checks, lookups and calculations are done before event handling is switched off. The "range of interest" is more complex in this example.

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Multithreading in Excel

    Thank you Guys very much!

    If Target.Cells.CountLarge > 1 Then Exit Sub
    this is awesome! Thank you, new thing learned

    I note you refer to the users having multiple instances of your master workbook open - is that a template, or are they using multiple instances of Excel?
    No, it is a template. They are creating copy only and open it.
    Waht you can suggest Rory?
    Do you think that enablevents it is a cause or maybe you have another idea?

    Jacek

  10. #10
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Multithreading in Excel

    The only things I know of that can stop events from running are enableevents and faulty UDFs used in conditional formatting.

  11. #11
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Multithreading in Excel

    Thank you Rory!

    Hmm enableevents it is my thing here, thank you TMS!
    What you mean faulty UDFs in conditional formatting? Do you have any example?

    Jacek

  12. #12
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Multithreading in Excel

    I simply mean that if you use a UDF in a conditional formatting formula, and it causes an error, that can terminate all event code. As a rule, it's best to avoid UDFs that use any properties of cell other than value or formula in a conditional formatting rule.

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Multithreading in Excel

    You're welcome. Thanks for the rep.

  14. #14
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Multithreading in Excel

    Thank you Guys!

    Jacek

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Multithreading in Excel

    Again, thanks for the rep. I suspect the critical thing is not how long the routine takes to execute but what, if anything, could cause it to fail during execution. If your normal practice is to switch off event handling as the very first thing you do, and switch it on again as the very last thing you do, there is scope for an error to crash the event handler and exit before switching event handling back on.

    If you want a more detailed answer, you would need to share (all) the code and/or a copy of the template.

+ 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. [SOLVED] Simulating multithreading in vba
    By abousetta in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-23-2012, 01:27 PM
  2. Calculation Optimization via Multithreading
    By Telefonica in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-10-2011, 11:07 AM
  3. will 2007 multithreading capability help the vba<-->worksheet tran
    By larry godfrey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-29-2006, 04:00 PM
  4. Multithreading in Excel
    By Paul in forum Excel General
    Replies: 3
    Last Post: 09-01-2005, 02:05 PM
  5. Bug in Excel? Excel Multithreading ERROR
    By JCChin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2005, 01:06 PM

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