+ Reply to Thread
Results 1 to 16 of 16

Incredibly Cumbersome Code!

  1. #1
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Incredibly Cumbersome Code!

    Greetings again, Gurus. It's 9:00 pm on Saturday night here in good ol' Tennessee, and I'm still working. Ain't life grand?

    Here's my deal. The workbook I have attached gives me the exact end results I want, but since most of the formatting was recorded, the code is incredibly long. I'm guessing one of you guys will have a field day, and laugh your **** off, reviewing this code.

    My goal is that the buyer take the one master sheet they receive from the supplier, extract the appropriate information needed to two seperate sheets and format them to print neatly. The buyers will use the printed sheets to go into the purchasin program on their computers and make changes to the purchase order. The page set up is so cumbersome because the original worksheet received is never formatted the same, some of these are coming from China, and even the type of paper is set wrong.

    The workbook is attached. If you guys want to take a crack at cleaning it up a little, I would appreciate it.

    Have a good one!

    Jerry
    Attached Files Attached Files

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    That code is using Select too much. It's seldom nessecary to select a range to work with it. For example, the posted
    Please Login or Register  to view this content.
    can be replaced with
    Please Login or Register  to view this content.
    Not using Select both speeds up the code and makes it more stable.

    The lines setting properties to their default values (eg.
    Please Login or Register  to view this content.
    can also be omitted.
    Last edited by mikerickson; 04-19-2008 at 10:43 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    How's this?

    Thanks, Mike. I'll clean up the 'set title' section for each sheet. If I'm correct, and get rid of defaul formats, the code below should work:

    Please Login or Register  to view this content.
    That tip will help trim it down some. Any other suggestions? Anybody?

  4. #4
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Revised Code

    I've cleaned up the code to the best of my ability, using Mike's tips as a guide. I've tried several other little changes, but I usually seem to screw up my output.

    I've attached the workbook, if anybody wants to take a look at it, and here is the code.

    Please Login or Register  to view this content.
    Thanks for any help or suggestions offered.

    Jerry

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Do you en sheets formatted the same?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    formatting

    Yes, Roy, both of my end sheets are fromatted the same.

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Then create a separate procedure for the formatting & then call it for each sheet. It might not be quicker, but it will be tidier.

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Quote Originally Posted by [email protected]
    I've cleaned up the code to the best of my ability, using Mike's tips as a guide. I've tried several other little changes, but I usually seem to screw up my output.

    I've attached the workbook, if anybody wants to take a look at it, and here is the code.



    Thanks for any help or suggestions offered.

    Jerry
    Hi,

    Just a general observation. Clearly a lot of that code was captured through the macro recorder. That's fine when starting out or getting the syntax for sonething you can't quite remember, but the big nuisance with the recorder is that it captures absolutely every parameter available and uses the default setting property. I find that most of the time default properties are fine and hence you can delete just about everything you've recorded apart from any where you don't want the default.

    You commented about why is took so much code to set the border. That's a classic case. The recorder captures all four sides of a border. If you're happy with a fours sided border then replace all the four sets of code with
    Please Login or Register  to view this content.
    It is quite a long procedure and I suggest for readability if nothing else it would be better broken down into separate procedures, calling them one by one from a master control procedure, or in a chain from each procedure in the chain.

    HTH

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Is it possible to manually format a sheet which can be used as a template sheet? Copy this sheet then rename it instead of adding blank sheets.

  10. #10
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Master Sheet

    I suppose it would be possible to format a sheet with the correct column widths and such in the workbook the sub will be called from. I would still have to select the "data cells" in each sheet, after the filtered data is imported, to format them with borders, as that range will never be the same.

    Also, regarding the .BorderAround procedure, that won't work, as I'm not really placing a border around the data, rather using borders to seperate the data for easy readability. Is there a shortcut for placing a border around every cell selected, like the grid, rather than just a border around the outside of the entire selection? This would give me nearly the same result, with the exception that I don't currently place a border around the outside of the selected range.

    Thanks for the help guys.

    Jerry

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Quote Originally Posted by [email protected]
    I suppose it would be possible to format a sheet with the correct column widths and such in the workbook the sub will be called from. I would still have to select the "data cells" in each sheet, after the filtered data is imported, to format them with borders, as that range will never be the same.

    Also, regarding the .BorderAround procedure, that won't work, as I'm not really placing a border around the data, rather using borders to seperate the data for easy readability. Is there a shortcut for placing a border around every cell selected, like the grid, rather than just a border around the outside of the entire selection? This would give me nearly the same result, with the exception that I don't currently place a border around the outside of the selected range.

    Thanks for the help guys.

    Jerry
    OK, instead of the .BorderAround, how about:

    Please Login or Register  to view this content.
    Rgds

  12. #12
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Maybe

    Please Login or Register  to view this content.
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Jerry, I don't think your code actually borders the cells.

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try this, it's abit tidier

    Please Login or Register  to view this content.

  15. #15
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Thanks

    Thanks, Guys. Roy's macro does everything, except that it titles both sheets "Quantity Changes" in cell F3. I can pull that part out of the "format" sub, and put it into the "filter" sub, add the code to title the other sheet "Price Changes" and I'm good to go.

    Have a good one guys.

    Jerry

  16. #16
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Hi Jerry, glad it worked.

    I noticed the problem but forgot to change it after sorting out the cell borders.

    Change it like this
    Please Login or Register  to view this content.

+ 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