+ Reply to Thread
Results 1 to 41 of 41

Improve efficiency of VBA Code

  1. #1
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Improve efficiency of VBA Code

    Hi,
    Row number 9 in this Attached example contains many formulas, These formulas will be converted to its values From row number 11 to last row of data.
    Macro is running fine but it takes more time with large amounts of data.Is there a more efficient and quicker way to do?
    I would appreciate any help that can be offered by way of the best approach to such a task.
    please see what I have so far. Thanks in advance.
    Attached Files Attached Files

  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,440

    Re: Improve efficiency of VBA Code

    Can't see much to do. Maybe a bit neater ...

    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
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    Thanks a lot for your reply
    There is not much difference
    I respect for your kind time and help
    Last edited by jonsonbero; 05-15-2020 at 06:58 PM.

  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,440

    Re: Improve efficiency of VBA Code

    You're welcome.

  5. #5
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,004

    Re: Improve efficiency of VBA Code

    Your formula are something like this:
    Please Login or Register  to view this content.
    so basically if col A is blank then blank otherwise put some number or text.

    Are those your actual formula? or you actually have different and more complicated formula?
    If those are your actual formula, then I suggest you create a macro to put the value (100,"very good" etc) directly without a formula.
    So basically:
    1. sort data by col A (from the header), so the blank ones will be in the bottom.
    2. use a macro to insert the number or text to the intended columns (in the rows where col A isn't blank)

    Let me know if you need help.

  6. #6
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    Thanks a lot for your reply
    I actually have different and more complicated formulas
    The idea is to convert formulas to their values depending on many conditions through many drop-down lists and many fixed data.
    Is there a way that I could speed up the running of the code.
    Thanks for any help you may have

  7. #7
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,004

    Re: Improve efficiency of VBA Code

    Well, turning off and on some features might help a little:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    Thank you so much
    I tried these lines but I didn't get the desired and everything is scrambled.

  9. #9
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    All help is much appreciated.

  10. #10
    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,440

    Re: Improve efficiency of VBA Code

    Remove the lines for Application.Calculation

  11. #11
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    welcome back Mr. TMS
    Unfortunately the same problem.I've been stuck on this for a few days.
    The code is working fine on the Sample attached but as for the original data, it takes about 70 seconds.
    but as I really don't know what I'm doing ... I thought I'd ask here.
    Thanks a lot for your guidance

  12. #12
    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,440

    Re: Improve efficiency of VBA Code

    It would be more helpful if you could post a desensitized version of the actual workbook with the real formulae.

  13. #13
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    The sample data set I provided is just for illustration purposes, As for the real file, I'm not allowed to post it online.
    I really hope you have some prepared macro code for this kind of problem.

  14. #14
    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,440

    Re: Improve efficiency of VBA Code

    I really hope you have some prepared macro code for this kind of problem.
    No. I can’t help. Not even sure what the problem is. As in many cases, we're second guessing based on a simplistic example and, for whatever reason, any solutions proposed don't help, or make things worse.

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Improve efficiency of VBA Code


    Hi,

    this is not a problem as there is no error, this is just how Excel was designed, can't be as fast as any database software.

    Maybe with a smart enough attachment well reflecting the original workbook
    - as the initial attachment is poor, very few data, can't help as expected -
    someone may see the light …
    Last edited by Marc L; 05-16-2020 at 07:27 PM.

  16. #16
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Improve efficiency of VBA Code

    Quote Originally Posted by jonsonbero View Post
    The sample data set I provided is just for illustration purposes ...
    Try this modification of your code and the above suggestions from colleagues (TMS, Akuini).
    I don't think it would be faster but you can check, nothing to lose ...
    Attach samples of real formulas, maybe they can be replaced with regular code.
    Please Login or Register  to view this content.

  17. #17
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    According to the initial attachment a VBA demonstration without the useless (but as calculation time is execution time) :

    PHP Code: 
    Sub Demo1()
            
    Dim Rg As Range
            Application
    .ScreenUpdating False
        With sheet1
    .UsedRange.Rows("3:" sheet1.UsedRange.Rows.Count).Columns
            
    For Each Rg In .Rows(-1).SpecialCells(xlCellTypeFormulas)
               .
    Item(Rg.Column).Value2 = .Parent.Evaluate(Replace(Rg.Formula"A9", .Item(1).Address))
            
    Next
        End With
            Application
    .ScreenUpdating True
    End Sub 
    ► Do you like it ? ► ► So thanks to click on bottom left star icon « Add Reputation » ! ◄ ◄

  18. #18
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Improve efficiency of VBA Code

    Possibly...?
    Please Login or Register  to view this content.
    Last edited by dangelor; 05-16-2020 at 10:25 PM.

  19. #19
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    Thanks to all of you
    First of all, I think the general idea of the topic is clear enough and Regardless of the attached sample And real formulas.
    Secondly, the codes presented are very wonderful and excellent but didn't work well with large amounts of data
    which is about 15000 rows it takes quite long (50 -70 second) ... so I am searching for alternative approach using arrays for example ( So to speak )
    or What is the best approach to achieve that in faster way ..... this is my problem as a beginner.
    Thanks all for any help I get
    Regards to all
    Last edited by jonsonbero; 05-17-2020 at 10:37 AM.

  20. #20
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Improve efficiency of VBA Code


    First, codes well work with big data, just Excel is obviously slow …

    As my post #17 procedure allocates directly the array result to each necessary column
    then you must think about another array strategy without any Excel formula
    depending if you want to hardcode the necessary columns or create a Settings worksheet to parameter each column …

  21. #21
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Improve efficiency of VBA Code

    Quote Originally Posted by jonsonbero View Post
    ... the general idea of the topic is clear enough and Regardless of the attached sample And real formulas ...
    It's not true - knowing the real formulas we could possibility (of course not in every case) replace these by something more effective.

    Quote Originally Posted by jonsonbero View Post
    ... codes ... didn't work well with large amounts of data ... about 15000 rows ... quite long (50 -70 second)
    Based on your "hypothetical" formulas and data, at 56 thousand rows (file xls about 30 MB), after testing, the first macro modification is performed in about 6 seconds. As you can see, the excel sheet is just slowed down by your real formulas.
    Last edited by mjr veverka; 05-17-2020 at 11:12 AM.

  22. #22
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Improve efficiency of VBA Code


    Yes it's fast enough on my side on an old slow computer …

    And as any 'full array' strategy needs exactly the same time than my post #17 procedure to write the array result to the cells,
    the difference is just on the time calculation and as I yet met 'full VBA' codes running slower than procedures using Excel features …

    As that also obviously depends on all we can't guess …

  23. #23
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Improve efficiency of VBA Code

    Quote Originally Posted by jonsonbero View Post
    ... quite long (50 -70 second) ...
    Marc L's macro takes about 4 s (... "hypothetical" formulas and data, at 56 thousand rows ...)

  24. #24
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    I tested it on original data and it is ok but took some time
    it seems I can't get it as a whole issue
    Best and kind regards for all who shared this issue

  25. #25
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Improve efficiency of VBA Code


    Far not an issue as the usual Excel behavior : calculation needs some time …

    Maybe if you answer to previous questions and elaborate a new strategy someone may give it a try.

  26. #26
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    I will try that

  27. #27
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    Hi,
    I couldn't do anything to persuade the manager to publish the real formulas.
    This is a newer version of the code but it is painfully slow.
    Can anyone help with some hints regarding how to optimize the attached code to run faster?
    Appreciate any help in this regards.
    Attached Files Attached Files

  28. #28
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    I have posted at this link too
    http://www.vbaexpress.com/forum/show...545#post402545
    Thanks for any help you may have.

  29. #29
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Improve efficiency of VBA Code


    As there is nothing new in your last attachment so just amend your code according to post #17 …

  30. #30
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    Thanks for your reply Mr. Marc L
    Hereby I attached an Excel file that almost matches to my actual file With your code.
    Please Login or Register  to view this content.
    I changed this line to
    Please Login or Register  to view this content.
    but something happened ... Please have a look at the example in "main workbook" to see what I mean
    note:- That yellow cells in original file would contain formulas
    Your help will be really appreciated. Thanks in advance.
    Attached Files Attached Files
    Last edited by jonsonbero; 05-22-2020 at 11:27 PM.

  31. #31
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Improve efficiency of VBA Code


    As this is not the same kind of formulas than your initial attachment
    you just forgot to replace AC6 in the genuine formulas like for A6 …

  32. #32
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    I am sorry that I could not understand you properly, what do you mean

  33. #33
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Improve efficiency of VBA Code


    Just compare the Excel formula in row #6 with the formula as it must be for the needed range …

    The other issue is the used range does not start in row #6 but in row #2 so you have to update the addresses in the code
    or make the worksheet smart as it must be just starting in row #1, obvious and easier …

  34. #34
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    Thank you for taking time and helping me to find a solution, Although the code is easy, but it's still a bit blurry to me ... So how I can fix that?

  35. #35
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Improve efficiency of VBA Code


    If you have difficulty to manage Excel formulas the better is to stay with your original code as after all, it well works …

    Other obvious ways is to not transform any formula like allocating directly formulas in the right places rather than above the headers
    or use a Settings worksheet with each appropriate formula needed …

  36. #36
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    Is there a way to improve it ..? or what is in the code that makes the code runs slowly?

  37. #37
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Improve efficiency of VBA Code


    On Excel side it runs normally, no gearbox so an unique speed …

    Another way for some developers is to not use any formula but hard code all the calculation using arrays …
    But in case of any smallest change you will have to rewrite the procedure so it depends on your Excel / VBA skills …

    Imagine the boss asking you a small change, for some reason you have only five minutes and no web access, will you be able ?
    Versus your original code which does not need any mod as it uses worksheet formulas so you have five minutes to amend a formula, doable …

  38. #38
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Improve efficiency of VBA Code

    Cross-posted - without acknowledging the extensive help already given here - at: http://www.vbaexpress.com/forum/show...cy-of-VBA-Code
    Please read Excel Forum's Cross-Posting policy in rule 8: http://www.excelforum.com/forum-rule...rum-rules.html
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  39. #39
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Improve efficiency of VBA Code


    Last tip from obvious Excel rules since 2007 version :

    use an Excel table as each time a new row is created the previous formulas are automatically added
    so a VBA procedure is very not necessary …

  40. #40
    Registered User
    Join Date
    05-14-2020
    Location
    London
    MS-Off Ver
    2010
    Posts
    17

    Re: Improve efficiency of VBA Code

    Thanks very much Mr. Marc L for the advise and for your interest in helping me.
    I feel I am idiot as a novice ....That was a little oversight on my part when I set up a smaller sample file to simulate the requirements.It's my fault.
    so please accept my apologies ....This is another sample that contains your code and my code
    Please run your code first to see what I mean... Thank you so much for your efforts on helping me with this topic.
    Attached Files Attached Files

  41. #41
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Improve efficiency of VBA Code


    As my code was based upon your initial attachment with has an unique address in all formulas
    the reason why it can't work anymore with your last attachment …

    There are some workarounds as yet explained previously but stay without any reaction neither any comment …

    The question is why this worksheet is so badly builded ?!
    How the data are created within this worksheet ?
    'Cause just using an obvious Excel table - as you can see in Excel help - no code is necessary ! Nothing to do …

+ 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. Help to improve code efficiency
    By Jactey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-10-2015, 02:08 AM
  2. [SOLVED] Long VBA Code - Improve Efficiency?
    By Dgp2012 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-18-2013, 11:29 AM
  3. [SOLVED] To improve Efficiency of code, code running too long
    By andywsw in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-19-2012, 05:54 PM
  4. Improve efficiency of code
    By blinks in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-08-2011, 06:42 PM
  5. Removing .Select to Improve Code Efficiency
    By Mordred in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-25-2010, 01:47 AM
  6. Improve code efficiency
    By Eggert in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-02-2009, 07:27 AM
  7. pivottables: would like to improve efficiency
    By lalbatros in forum Excel General
    Replies: 0
    Last Post: 09-03-2007, 09:14 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