+ Reply to Thread
Results 1 to 17 of 17

Advice on how to improve the attached macro (and tips)

  1. #1
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    wales
    MS-Off Ver
    Excel 2007
    Posts
    964

    Advice on how to improve the attached macro (and tips)

    attached please find a workbook with a macro.

    the macro is used to copy and paste data , clear columns and create a data sheet.

    it works and does what i need it to however id like to know if i can compact it or make it more efficent.

    i am a VBA noob and i am sure that it can be improved. here is the code

    Please Login or Register  to view this content.


    if you can improve it please can you show me how and try to explain the alterations a little to allow me to understand better for future development (as this will be an ongoing project)

    regards

    steve

    Heart of Crafts Master (Finished).xlsm
    The Importance of INDEX - A GUIDE TO INDEX'S OTHER USES
    <--- If a post helps hit the star

  2. #2
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    wales
    MS-Off Ver
    Excel 2007
    Posts
    964

    Re: Advice on how to improve the attached macro (and tips)

    gentle bump

    please any advice greatly received

  3. #3
    Forum Contributor
    Join Date
    08-20-2012
    Location
    Walsall,England
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Advice on how to improve the attached macro (and tips)

    I don't think your variables are declared properly:
    Dim DynPaper, DynStamp, DynDia, DynPen As Integer

    this only delcares DynPen as integer, you haven't declared the others so they default to variant data type (this occupies more memory).
    If you want to declare them all as integer you need:

    Dim DynPaper As Integer, DynStamp As Integer, DynDia As Integer, DynPen As Integer

    Ant

  4. #4
    Forum Contributor
    Join Date
    08-20-2012
    Location
    Walsall,England
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Advice on how to improve the attached macro (and tips)

    I don't think your variables are declared properly:
    Dim DynPaper, DynStamp, DynDia, DynPen As Integer

    this only delcares DynPen as integer, you haven't declared the others so they default to variant data type (this occupies more memory).
    If you want to declare them all as integer you need:

    Dim DynPaper As Integer, DynStamp As Integer, DynDia As Integer, DynPen As Integer

    Ant

  5. #5
    Forum Contributor
    Join Date
    08-20-2012
    Location
    Walsall,England
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Advice on how to improve the attached macro (and tips)

    Also I think it's best if variables are declared at the top of the macro so they are all together therefore easier to edit.

  6. #6
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    wales
    MS-Off Ver
    Excel 2007
    Posts
    964

    Re: Advice on how to improve the attached macro (and tips)

    ok thanks ill alter that.

    any one else any pointers tips or pieces of advice?

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Advice on how to improve the attached macro (and tips)

    Id get rid of all your loops to find the last row. And use what in Bold below

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Advice on how to improve the attached macro (and tips)

    I would suggest altering the code names of the sheets in the vbeditor to something meaningful and then using those in your code where possible instead of relying on the tab names. also you can use faster methods to find the last used row in a column and turning off screenupdating will speed things up and avoid flicker
    Please Login or Register  to view this content.
    assuming the relevant adjusted code names
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  9. #9
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    wales
    MS-Off Ver
    Excel 2007
    Posts
    964

    Re: Advice on how to improve the attached macro (and tips)

    @ mike

    thanks for that. only problem is that that will pick the entire column as there is a formula in there to fill it out
    (like =IF(A1="","",B1)

    which I suppose doesn't matter to much as its only pasting the values. but what would be faster?


    @ Joseph

    the anti flicker is great

    how do you mean about editing names? do you mean to set them at the top so
    wbPaper = sheets.("Paper")

    thanks people.

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Advice on how to improve the attached macro (and tips)

    no-I mean to edit them in the vb editor-see picture
    Attached Images Attached Images

  11. #11
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    wales
    MS-Off Ver
    Excel 2007
    Posts
    964

    Re: Advice on how to improve the attached macro (and tips)

    ok i get you thanks for that.

    through the tips here i have updated my code. it feels faster (the main thing is the flicker)

    here is the new code. any more suggestions before i sign it off?

    Please Login or Register  to view this content.

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Advice on how to improve the attached macro (and tips)

    you have a couple unqualified range references
    Please Login or Register  to view this content.
    ought to be
    Please Login or Register  to view this content.
    note the 2 additional periods before Range and Cells

    you could make other minor tweaks but nothing significant I can see beside the fact that using end(xlup).row is faster than looping down a column to find the last used cell

  13. #13
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    wales
    MS-Off Ver
    Excel 2007
    Posts
    964

    Re: Advice on how to improve the attached macro (and tips)

    Quote Originally Posted by JosephP View Post

    you could make other minor tweaks but nothing significant I can see beside the fact that using end(xlup).row is faster than looping down a column to find the last used cell
    the problem with xlup.row it still pick the cells that are empty as they have been used (formulas / conditional formating etc) so it sets the print area to 500 rows long. is there a work around?

  14. #14
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Advice on how to improve the attached macro (and tips)

    only not populating rows with formulas unnecessarily ;-)

  15. #15
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    wales
    MS-Off Ver
    Excel 2007
    Posts
    964

    Re: Advice on how to improve the attached macro (and tips)

    i assumed that the code would remove the formulas prior to this when it does this

    Please Login or Register  to view this content.
    but when i tried the xlup it still went right down? can you think of any reason (conditional formatting etc) that might cause this?

  16. #16
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Advice on how to improve the attached macro (and tips)

    if you copy and paste special a formula that returned "" the cell is not treated as empty. either keep the loop you have or don't populate the source sheet with formulas past the end of your data or only copy the data based on the usedrange from a different column-again using end(xlup)

  17. #17
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    wales
    MS-Off Ver
    Excel 2007
    Posts
    964

    Re: Advice on how to improve the attached macro (and tips)

    ill leave the loop in as those columns are all dynamic and could grow or shrink (thats whay i have that in there)

    thanks for all your help much appreciated.

    i feel a little clearer about VBA now (this is my first real attempt at writing a decent macro)

    regards

    Stephen.

+ 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