+ Reply to Thread
Results 1 to 12 of 12

Adding ranges with VBA code depending on Form Control

  1. #1
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Adding ranges with VBA code depending on Form Control

    Hi all,

    Is there a particular reason why below code runs for 25 seconds? Can it be optimised somehow? The range aren't calculating anything, so far I have simply "added" them to the workbook...

    Please Login or Register  to view this content.

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Adding ranges with VBA code depending on Form Control

    Hi,

    Why are you giving the same ranges two names?

    FYI, I find it easier to use this syntax
    Please Login or Register  to view this content.
    and you may want to make calculation manual while you add the names and, if the TimePeriod_Analytics combobox is directly linked to a range using ListFillRange, you should probably use a flag variable to prevent recursion just in case.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Adding ranges with VBA code depending on Form Control

    Why are you giving the same ranges two names?
    Because if the fiscal year 2016 is selected in my form control combobox, I need last years data to refer to the same as this years data as 2015 data is unavailable.

    and you may want to make calculation manual while you add the names and, if the TimePeriod_Analytics combobox is directly linked to a range using ListFillRange, you should probably use a flag variable to prevent recursion just in case.
    Not sure what you mean by "flag variable"? My combobox cell link is the "Fiscal_Year_Analytics" range as defined in my code.

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Adding ranges with VBA code depending on Form Control

    I was referring to the list in the combobox- where does that come from?

    Did you try turning calculation to manual to see if that speeds things up?

  5. #5
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Adding ranges with VBA code depending on Form Control

    The list in the combobox is coming from my "Control" sheet, the cell highlighted in yellow = Worksheets("Control").Range("Fiscal_Year_Analytics")

    Did you try turning calculation to manual to see if that speeds things up?
    Yup, just tried. If I use "Application.Calculation = xlCalculationManual" alone, it speeds it up to about 3-4 seconds (instead of 25 seconds). However previously I used my "optimise code" macro (see below) and it did not speed up anything...hmm
    3-4 seconds isn't too bad, but not ideal, as I still need to add some code. But I assume there's no more room for speed improvement?

    Please Login or Register  to view this content.
    forum.PNG

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Adding ranges with VBA code depending on Form Control

    Is the list for the combobox a named range or a static address? If it's named, is that a fixed address or a dynamic range?

  7. #7
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Adding ranges with VBA code depending on Form Control

    it's a static address that doesn't change!

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Adding ranges with VBA code depending on Form Control

    OK, then a flag variable probably wouldn't make any difference.

    Your OptimizeCode_End code is wrong though- you should remove the red line
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Adding ranges with VBA code depending on Form Control

    Please Login or Register  to view this content.
    But I need to turn automatic calculation back on at the end of the code??

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Adding ranges with VBA code depending on Form Control

    Note the blue line preceding it
    Please Login or Register  to view this content.
    which already resets the calculation state to whatever it was before you started running your code, and is better practice than blindly setting it back to automatic.

  11. #11
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Adding ranges with VBA code depending on Form Control

    Oops! Thanks for pointing that out! It's working now! I guess I have to live with the 3-4 seconds delay. Thanks for your help!

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Adding ranges with VBA code depending on Form Control

    You're welcome.

+ 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] Call macro depending on cell link of a Form Control Box
    By esbencito in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-27-2017, 05:09 AM
  2. Two Combo Boxes (Form Control) depending on each other
    By esbencito in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 09-22-2017, 01:25 AM
  3. [SOLVED] VBA Code to have a repeating form control /Active X control (List box) in every row
    By Ehezve in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-28-2013, 08:55 PM
  4. Adding up ranges depending on condition
    By sp_key in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-12-2013, 01:14 PM
  5. Adding a control to a User Form
    By scantor145 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-17-2005, 05:05 PM
  6. Adding code to control on form causes Excel to crash
    By skuzapo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-22-2005, 04:14 PM
  7. [SOLVED] Adding code to a control on a form causes excel to crash
    By Skuzapo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-21-2005, 10:05 AM

Tags for this Thread

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