+ Reply to Thread
Results 1 to 7 of 7

Code Writing Tips

  1. #1
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Code Writing Tips

    I've decided I am going to go through code that I have from projects I have completed and I wanted to get some advice on better writing techniques. I'll give two examples that do the same thing (clean my insert page) from all my projects.

    Original code:

    Please Login or Register  to view this content.
    Or, is this a better, smoother way:

    Please Login or Register  to view this content.
    I think the second way is easier on the eyes but does it make any difference as far as how the code runs?

    Thank Ya Big Big
    Last edited by Mordred; 08-17-2010 at 08:28 AM.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Code Writing Tips

    Try this
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.

    Either should do the same.

    Hope this helps
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  3. #3
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Code Writing Tips

    Wow Marcol, that was great! Just so I understand

    Please Login or Register  to view this content.
    (forgive my terminology here), but that function has all of the lines of code that I had used in it already? Either way, that is going to be very useful to me. Thanks so much.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Code Writing Tips

    As Marcol has demonstrated, the Macro Recorder gives real garbage for many events you record. The best time to edit down a macro is immediately after you record it.

    If you record yourself simply adding a border to the bottom of a cell and bolding the cell, you get a 15+ lines of code, when you know you only did two things...sounds like it should only be two lines, yes? So as you review the edited code you can pretty safely edit lines that don't have to do with what you recorded.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Code Writing Tips

    I did just what you said about recording the process of adding a line to the bottom of a cell. It outputs in the module a lot of fluff and stuff that is default like xlNone references. When I am at work tomorrow I am going to make quite a few changes to my finished projects. Thanks JB.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Code Writing Tips

    Just one small point: there is a big difference between the code you posted originally and the Clear method and, depending on your intentions, the latter may not be what you want.
    Your initial code targets certain specific formatting and contents; Clear removes everything, including data validation and conditional formatting. No real way of us knowing from your post whether that was your intention.
    A literal translation of your original would I think be:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Code Writing Tips

    Hey romperstomper, fortunately, the clear method is perfect for my use. The method is run when the workbook closes so that the insert sheet is clear of all formatting and values. It is an insert sheet where data from our source is originally pasted, to update our records. The tables we bring in from the source are full of borders, interior colors, and merged cells, all of which need to go at close. Thanks for your insight though, just knowing that I don't have to enter all of the different border removals will help me in the future when I want to clear just borders.

+ 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