+ Reply to Thread
Results 1 to 19 of 19

How to improve this macro to run faster

  1. #1
    Registered User
    Join Date
    05-19-2013
    Location
    Sarawak
    MS-Off Ver
    Excel 2007
    Posts
    10

    How to improve this macro to run faster

    Hi all expert there,

    I'm new to this website and so with the macro thing. Recently, I learn on how to use macro in simulating reservoir for hydropower project. I have managed to prepared one but it too time consuming. Can someone out there to check if there is any improvement for my vba below? Your comment is highly appreciated.
    Please Login or Register  to view this content.
    Thanks,

    jerry from Malaysia.

    Sorry for my bad english.
    Last edited by Leith Ross; 05-19-2013 at 11:03 PM. Reason: Added Code Tags

  2. #2
    Valued Forum Contributor
    Join Date
    12-14-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    439

    Re: How to improve this macro to run faster

    Hi jerry,
    I want to see data and formulas in your file. Can you attach it?

  3. #3
    Registered User
    Join Date
    05-19-2013
    Location
    Sarawak
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to improve this macro to run faster

    Hi Huuthang_hd,

    I have tried to attach my file but failed to do so, do you have any email? so that I cand send the file to you.

    Thanks.

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: How to improve this macro to run faster



    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: How to improve this macro to run faster

    Hi and welcome to the forum

    I'm afraid your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  6. #6
    Registered User
    Join Date
    05-19-2013
    Location
    Sarawak
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to improve this macro to run faster

    Hi all,

    I'm sorry for that as I did not read the regulation thoroughly for cross posted my question. I just send my question to the website as above only. No more than that.

    Anyway, the problem is unsolved yet.

    Sixthsence,
    I tried to follow your instruction to attach my file but still failed to upload. there is a notice that "upload of file is failed".

    But do hope somebody can help me on this macro problem.

  7. #7
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: How to improve this macro to run faster

    To attach a file to your post,
    click advanced (next to quick post),
    scroll down until you see "manage file",
    click that and select "add files" (top right corner).
    click "select files" find your file, click "open" click "upload" click 'done" bottom right. click "submit reply"

    Once the upload is completed the file name will appear below the input boxes in this window.
    You can then close the window to return to the new post screen.

  8. #8
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: How to improve this macro to run faster

    I tried to follow your instruction to attach my file but still failed to upload. there is a notice that "upload of file is failed"
    Not sure what is the size of the file? If it is greater than 1 MB then zip it and try to attach

  9. #9
    Registered User
    Join Date
    05-19-2013
    Location
    Sarawak
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to improve this macro to run faster

    Dear FDibbins?Sixthsense,

    My file is more than 3mb size. However, I will try to attach the lite version with the formula. The difference is the one that I tried to attached before come with daily time series from 2000 to 2049. The one that I attach now is come with monthly time series. The vba module is same.

    If anyone can help me on this to make my macro faster, I really appreciate it. I intent to use it in my sheet that come with daily time series. I know there is a lot of software such as Hec-ressim that do a work for me, but I'm more comfortable working using spreadsheet.

    jerry
    Attached Files Attached Files

  10. #10
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: How to improve this macro to run faster

    Try this...

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    05-19-2013
    Location
    Sarawak
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to improve this macro to run faster

    Sixthsense,

    I got this error while running your code as above.

    By the way, thanks for the suggestion. Will look into it also.
    Attached Images Attached Images

  12. #12
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: How to improve this macro to run faster

    Not sure on which line? keep locals window on which tells more to identify the error...

    Check these values whether they are numbers or something else?

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    05-19-2013
    Location
    Sarawak
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to improve this macro to run faster

    The values above are number. But the values are refer to the tab before it.

    I'm still try to run it but always got no responding notice.

    I will try to work it out at this moment. I will update you if I'm able to run it with your code.

  14. #14
    Registered User
    Join Date
    05-19-2013
    Location
    Sarawak
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to improve this macro to run faster

    Sixthsense,

    I believe by adding these into the VBA code make the file is not responding. Once I take it out, my macro run as usual but still at low speed.
    Please Login or Register  to view this content.
    Last edited by vlady; 05-21-2013 at 10:43 PM.

  15. #15
    Registered User
    Join Date
    12-18-2009
    Location
    Leeds
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: How to improve this macro to run faster

    I'd imagine it 'stops responding' because it is running but not providing you with notifications. It will run faster, and complete eventually.

  16. #16
    Registered User
    Join Date
    05-19-2013
    Location
    Sarawak
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to improve this macro to run faster

    I hope that would be the case. But it showing at the above of my spreadsheet that it is not responding. That make me think that it is not running. The screen in white blur when iI move the cursor. Anyway, thanks for the reply.

  17. #17
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: How to improve this macro to run faster

    Maybe something like this:

    The bottleneck here is that you are reading and writing a million times to and from the excel sheet. That is very slow. I have converted them to reading only once and writing only once per variable y.

    Please Login or Register  to view this content.
    Hope this helps.

    abousetta
    Last edited by abousetta; 05-22-2013 at 05:34 AM. Reason: Adjusted code
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  18. #18
    Registered User
    Join Date
    05-19-2013
    Location
    Sarawak
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to improve this macro to run faster

    Thanks very much abousetta. I will try your code later as at the moment I'm attending 3 days conference at my place. I will update you with the outcome by running your suggested code in my excel sheet.

    Hope it will work this time.

  19. #19
    Registered User
    Join Date
    05-19-2013
    Location
    Sarawak
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to improve this macro to run faster

    abousetta,

    I tried to run your code and found this error.
    Run-time error '6':
    Overflow


    Line 15 is highlighted with yellow colour.

    Sub Button1_Click()
    Dim x&, y&, A&, B&, C&, D&, vE7Value&, vK13Value&, vK14Value&
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    x = Cells(18, 2).Value + 20
    vE7Value = Cells(7, 5).Value
    vK13Value = Cells(13, 11).Value
    vK14Value = Cells(14, 11).Value
    For y = 21 To x
    A = 0 ' First run plant discharge starts at 0.
    B = Cells(y, 6).Value
    C = Cells(y, 21).Value
    D = 0.98 * vK13Value
    Do While B >= vE7Value ' Current reservoir level above MOL
    A = A + 5 ' Increment @ 5.
    If C >= D Then Exit Do ' almost there.
    Loop
    Do While B >= vE7Value
    A = A + 0.1
    If C >= vK13Value Then Exit Do ' Rated power is achieved.
    Loop
    If C < vK14Value Then Cells(y, 8).Value = 0 Else Cells(y, 8).Value = A
    Next
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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