+ Reply to Thread
Results 1 to 79 of 79

Do's and Don'ts So Others Won't

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

    Post Do's and Don'ts So Others Won't

    I'm sure that you pros have many don'ts when it comes to VBA programming. A good example is the use of GoTo Labels (beaten to death I'm sure). If you have the time and you think of a good don't, please post it here.
    I would like to start off by asking about Option Explicit. I understand what it does ie forcing the programmer to define variables but is it necessary? I see some use it when they help others with code and some don't use it. When don't you and when do you use it?
    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 romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    ALWAYS use it. (set it in options)

    See also this thread.
    Last edited by romperstomper; 09-08-2010 at 04:42 PM.
    Remember what the dormouse said
    Feed your head

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Do's and Don'ts So Others Won't

    Use a tool (e.g., SmartIndenter) to indent your code. It is much easier to understand, and many compile errors can be avoided just by appearance.
    Entia non sunt multiplicanda sine necessitate

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

    Re: Do's and Don'ts So Others Won't

    @romperstomper, good link, thank you.
    @shg, where is something like SmartIndenter? I looked for it in 'Tools' but didn't see it.

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    You can get it here.
    Code Cleaner is another must.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Do's and Don'ts So Others Won't

    about Option Explicit.
    People who don't use it aren't programming, they're just playing with their computer.

    Code Cleaner is another must.
    Ditto that. I used to do manually what it automates; I cried when I found it. Not like I cried when I discovered VisiCalc, but a good cry nonetheless.

    I would add this, high on the list: adopt a variable naming convention and never deviate from it, unless you find one you like better.

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

    Re: Do's and Don'ts So Others Won't

    People who don't use it aren't programming, they're just playing with their computer.
    Too funny, I've mostly just been playing with my computer. I'm going to *** through my projects thus far and use Option Explicit. Once I am done, will I be an official programmer?

    I'm also going to look into the Code Cleaner you folks are talking about.

    You guys are awesome.

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by shg View Post
    I would add this, high on the list: adopt a variable naming convention and never deviate from it, unless you find one you like better.
    Agreed. In fact consistency generally in your programming will stand you in very good stead. (unless of course you are trying to remain anonymous on an internet forum: it took one member here 3 posts to work out who I am. )

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Do's and Don'ts So Others Won't

    Severely limit your use of global variables - they encourage very sloppy programming. Every sub and function should be stand-alone (subject to being passed suitable parameters).

    Don't code literal values, take the time to either define constants or, better still, pass parameters using the Optional keyword.

    Not long ago I would have coded:

    Please Login or Register  to view this content.

    Which means that if wanted to use that sub in another workbook I had to remember to move and set the relevant global constants (and Excel gets really cheesed off if you open 2 workbooks that define the same global constants.

    So I moved to:

    Please Login or Register  to view this content.

    Which is better, but lack flexibility and elegance, so I've progressed on to.

    Please Login or Register  to view this content.

    Much, much neater and completely transportable.

    Oh, and always remember that you virtually never have to select cells in VBA

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

    Re: Do's and Don'ts So Others Won't

    I just installed and ran the Code Cleaner on one my projects but what did it actually do? Everything looks the same.

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

    Re: Do's and Don'ts So Others Won't

    Oh, and always remember that you virtually never have to select cells in VBA
    I'm still trying to break the habit of selecting cells and ranges and what-not but my fingers think for them selves sometimes (cursed things).

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by Mordred View Post
    I just installed and ran the Code Cleaner on one my projects but what did it actually do? Everything looks the same.
    Everything should look the same (but maybe a little more sparkly). It cleans out the p-code garbage that builds up as you repeatedly edit VBProjects.

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

    Re: Do's and Don'ts So Others Won't

    SmartIndenter is sweet!

  14. #14
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Do's and Don'ts So Others Won't

    hi Modred,

    There's an overlap with what's already been offered, but here is a link to a list of some tools which I find handy: http://www.excelforum.com/2078299-post14.html

    Plus another freeware comparision tool that I've found quite helpful for comparing versions of VBA code (just being a self-taught chap, with no specific "version control app" which I've seen some IT guys use for other languages): http://www.formulasoft.com/vba-code-compare.html

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  15. #15
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Do's and Don'ts So Others Won't

    Never write a line of code like this:
    Please Login or Register  to view this content.

  16. #16
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    As if that could happen...

  17. #17
    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: Do's and Don'ts So Others Won't

    I wonder who did?......

  18. #18
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Do's and Don'ts So Others Won't

    Re Option Explicit, I always use it, just never quote it in my posts.

    I don't use any VBA tools but keep meaning to look at them as I gather there are some very useful ones.

    Do learn about passing values to and from arrays (sooner than me anyway, they still confuddle me).

    Don't keep a bottle of lighter fluid in the booze cupboard

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  19. #19
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by romperstomper View Post
    (unless of course you are trying to remain anonymous on an internet forum: it took one member here 3 posts to work out who I am. )
    I think I can probably guess who that might have been
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  20. #20
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Do's and Don'ts So Others Won't

    I suspect it was Richard Schollar... the same point holds true for formula bods also... everybody has a style to some extent (good/bad).

  21. #21
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by DonkeyOte View Post
    I suspect it was Richard Schollar.
    I actually would have guessed that it way you

  22. #22
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    It was indeed Richard. Unnerved me a bit.

  23. #23
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Do's and Don'ts So Others Won't

    Yeah, it is very hard to hide from your style...

    I also guessed a couple of members correctly....who had different names in other forums...

  24. #24
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Do's and Don'ts So Others Won't

    I assumed my whack it really hard with a hammer until it gives in style would have been transparent so saw little point in using a different name.

    Have we gone a little off topic here maybe?

    Dom

  25. #25
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by Domski View Post

    Have we gone a little off topic here maybe?

    Dom
    that tends to happen in many watercooler threads.... just like in real world watercooler conversations.... someone else comes into the lunch room and inserts different thoughts based on what they heard when they walked in...

  26. #26
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Do's and Don'ts So Others Won't

    Some of the tangents that the Lounge at Mr Excel used to go off on were interesting to say the least, usually thanks to a certain Greg Truby.

    Dom

  27. #27
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    But you have to admire his high-falutin' language.

  28. #28
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Do's and Don'ts So Others Won't

    Alright, pop-quiz hot shots. Do you re-use variables? If you have multiple (non-nested) loops, do you re-use i for integer?
    What if you use a string to find a workbook name using the application.getopenfilename dialog box. Would you redefine the string after opening the workbook to get rid of the path in the name?

    Examples:

    Please Login or Register  to view this content.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  29. #29
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Do's and Don'ts So Others Won't

    Personally, I wouldn't re-use a variable even in separate loops. If I ever needed to use multiple loops in code they would be doing different things, so I'd differentiate them to avoid any confusion. If this was 40 years ago and memory allocation was more of an issue, perhaps.

  30. #30
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Do's and Don'ts So Others Won't

    I reuse scratch variables if their scope of use is within a few lines of code, and I identify them as such in the declaration (I comment all my variable declarations as to purpose when I'm not just playing with my computer).

    For variables that index arrays, I name them consistent with the array; iChr indexes asChr, which has nChr elements.

    I wouldn't reuse your string variable below because in the first case it's a path and filename, and in the second, just the filename.

  31. #31
    Forum Contributor
    Join Date
    05-16-2010
    Location
    UK
    MS-Off Ver
    2003 and 2007
    Posts
    252

    Re: Do's and Don'ts So Others Won't

    Going off at a tangent (apologies) makes me think of this:

    http://www.excelforum.com/the-water-...urely-not.html

    Is there an Option Non-Explicit?

    Anyway, I am guilty of not using Option Explicit and on top of this I have not posted in the Excel Programming Forum yet (respect to Mordred for having the courage to do so), but I am observing and learning (I hope).

    Phew, I feel a weight has been lifted from my shoulders!

    I would just like to say (from someone who is self-taught like many others) that this topic is a revelation (to me anyway). I wouldn't say I was playing (I didn't know any better). Thanks shg (and I understand your totally justified comment ... now)! Good on you Mordred, for having the ..... to ask what so many of us would like to (without fear of the repercussions).

  32. #32
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Do's and Don'ts So Others Won't

    Here's a tip that I've just remembered - read about what the ByVal and ByRef arguments mean in function definitions and apply them.

    Some time ago I wrote what I thought was a straightforward function to convert a number to binary ...

    Please Login or Register  to view this content.

    The code that called this function then went on to do other things with the binary number returned and the decimal equivalent, except it kept crashing out as if the decimal number was zero, even though I'd already checked for that condition. It took me *ages* to figure out what the problem was.

    I know now that it's bad practice to have a function change any parameter passed to it, but because of that mistake I appreciate why.

  33. #33
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    Oh one other one:
    If you are not using the Call keyword, or returning a value from a Function, do not enclose arguments in parentheses.

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

    Re: Do's and Don'ts So Others Won't

    do not enclose arguments in parentheses
    Why? Is that a style preference? I come to actually like parentheses around arguments as it (in my mind) lets me know exactly where an argument is being passed.
    @pb71, I'm one for asking questions. I like to know the whys because I think they are necessary if you want to do well with whatever task at hand. Knowing how isn't enough for me.

  35. #35
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    No it's syntax not style. Try it with an object variable.
    For example:

    Please Login or Register  to view this content.
    Last edited by romperstomper; 09-10-2010 at 04:10 AM. Reason: add examples.

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

    Re: Do's and Don'ts So Others Won't

    You'll have to parden my naivety romperstomper, but you are using parentheses in your last example and so I am a little confused. Specifically where it works ie:
    Please Login or Register  to view this content.
    Could you elaborate a little more regarding this?

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

    Talking Re: Do's and Don'ts So Others Won't

    I just wanted to thank you guys, especially Andrew, who has steered me regarding using .Select. I took this code,

    Please Login or Register  to view this content.
    and changed it to this code,

    Please Login or Register  to view this content.
    It was not easy and took me a fair bit of time to do but I'm glad I did it because I learned a lot while fighting throught the code to make it work. It also reduced the total time to update by about 35 seconds (which seems like forever when you are waiting for code to finish).

  38. #38
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by romperstomper View Post
    If you are not using the Call keyword, or returning a value from a Function, do not enclose arguments in parentheses.
    Note the bold part.

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

    Re: Do's and Don'ts So Others Won't

    Originally Posted by romperstomper
    If you are not using the Call keyword, or returning a value from a Function, do not enclose arguments in parentheses.
    Right, I did read that once already didn't I? I'll just slap myself for you since you can't do it from where you are.

  40. #40
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Do's and Don'ts So Others Won't

    Oh yeah?

    Please Login or Register  to view this content.

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

    Re: Do's and Don'ts So Others Won't

    .Run "Invite_Mother_In_Law_To_Dinner"
    Noooooooooooooo!

  42. #42
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by Mordred View Post
    I just wanted to thank you guys, especially Andrew, who has steered me regarding using .Select.
    Thanks, Mordred, it's nice to know I've helped. On the other hand, I've been using Excel for 15 years and have been the Excel expert in all of the companies I've worked in during that time, but I'm in awe of some of the people on this forum (and in this thread) - you never stop learning.

  43. #43
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Do's and Don'ts So Others Won't

    Hi all,

    Andrew, I agree, "never stop learning"

    Mordred,
    I'm about to take the thread slightly off track again - the joys of the Watercooler eh?

    Quote Originally Posted by Mordred View Post
    ...It was not easy and took me a fair bit of time to do but I'm glad I did it because I learned a lot while fighting throught the code to make it work. It also reduced the total time to update by about 35 seconds (which seems like forever when you are waiting for code to finish).
    That's a great speed improvement & the code looks better too
    How fast is it now?
    I think we could make your code even faster with a few more tweaks.
    - To save some typing time you could incorporate GetOpenFileName into your code (see here or here for Jacob's examples). However, if you have Conditional Formatting on your sheets then also have a read of this thread & the tinyURL's referring to Greg Wilson's findings.
    - Each time you interact between a spreadsheet & VBA slows a macro down. The below code seems to suggest that you are looping through every cell in a Range that is 12 columns wide.
    Please Login or Register  to view this content.
    It seems that you currently only work on one file at a time and leave that file open even when the macro finishes. If you are not making any permanent changes in the opened file (ie just grabbing the values), then you could:
    • open it as read only
    • record a macro that Finds & Replaces ".." with "" (perhaps even do this before setting the first range)
    • then use a combination of .end(xldown) &/or .end(xlright) to identify the size of the range
    • copy/transfer the range values
    • then close the file without saving
    - It seems as if nothing actually "belongs to" the below line object in the With statement, is it redundant?
    Please Login or Register  to view this content.
    - Also, (I think this is mentioned in the "What not to do in VBA" thread), each time you use "Set xyz = ..." in your code, many people recommend releasing the memory at the end of your code by using "Set xyz = Nothing" once you have finished using the object.


    hth
    Rob

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

    Re: Do's and Don'ts So Others Won't

    Hi broro,
    I timed it at just over 6 seconds to complete with an online stopwatch so there is a +- of about 2 or 3 seconds. There is no conditional formatting, it is just a straight movement and storage fo data. I am going comment this out:
    Please Login or Register  to view this content.
    to see if it makes a difference or not. As for the "..", the values to be updated are each month but, we don't have say August yet so the update from our source uses ".." instead of a numerical value. The user just changed his criteria a little so I am now working on a way to grab all the values in the range except for ".." and 4 other cells in the range. I've been working on a function that will help but it is still in progress. The code I finish with will probably not resemble the code I submitted on this thread.

    I really do appreciate your feedback though on that, no doot aboot it!

    Best Regards

  45. #45
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Do's and Don'ts So Others Won't

    hi Mordred,

    If you want any feedback on your final code, feel free to start a new thread asking for feedback & pm'g me a link to it (or even providing a link in this thread)

    Rob

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

    Re: Do's and Don'ts So Others Won't

    Here is more for the do or don't thread.
    While in VB editor, there is the option to insert a new module or, the actual worksheet can have code written into it. I guess my question is, when is it appropriate to insert code into the worksheet? I mostly use modules but I have inserted code into worksheets when I am designing a worksheet. Is there a do or don't for code in worksheets compared to modules? Some insight is appreciated as always.

  47. #47
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    As a general rule, event code (for the sheet and its objects) goes in a sheet (or workbook) module. Everything else goes in a normal module. There are different schools of thought, but they're wrong.

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

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by romperstomper View Post
    There are different schools of thought, but they're wrong.
    Thanks romperstomper, lol, I'll take your word as gospel on the matter then.

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

    Re: Do's and Don'ts So Others Won't

    @romper, nice carving by the way for your avatar. Did you carve it? It is a pumpkin right?

  50. #50
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    Yes, and yes. Thank you - my daughter wasn't impressed by any of them!

  51. #51
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Do's and Don'ts So Others Won't

    my daughter wasn't impressed
    Appreciation of art will not develop before the age of 3.

  52. #52
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by davegugg View Post
    Alright, pop-quiz hot shots. Do you re-use variables? If you have multiple (non-nested) loops, do you re-use i for integer?
    What if you use a string to find a workbook name using the application.getopenfilename dialog box. Would you redefine the string after opening the workbook to get rid of the path in the name?
    I still use J as a loop variable where the loop is just a counter, if I actually do anything with the loop variable a more meaningful name is appropriate!!
    Regards
    Darren

    Update 12-Nov-2010 Still job hunting!

    If you are happy with the results, please add to our reputation by clicking the blue scales icon in the blue bar of the post.

    Learn something new each day, Embrace change do not fear it, evolve and do not become extinct!


  53. #53
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: Do's and Don'ts So Others Won't

    If you just wanted the total of hours, add column D as a helper to your first sheet :-

    =Month(a1)

    then you can total for each month with :-

    =SUMIF(Sheet1!$D$2:$D$13,9,Sheet1!$C$2:$C$13)-SUMIF(Sheet1!$D$2:$D$13,9,Sheet1!$B$2:$B$13)

    Pivot table is the best way to go!!!!!!

  54. #54
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Do's and Don'ts So Others Won't

    and that answers what question squig?
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  55. #55
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: Do's and Don'ts So Others Won't

    oops, blush! erm it answered on question somewhere!

  56. #56
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: Do's and Don'ts So Others Won't

    Ok if the post below answers your question then YEY, otherwise oops!

  57. #57
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Do's and Don'ts So Others Won't

    I think you'll find the answer is 42

    Dom

  58. #58
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: Do's and Don'ts So Others Won't

    and the question then must be "What is your age?!"

  59. #59
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Do's and Don'ts So Others Won't

    I was very disappointed in the final book of that series. Sighhhh
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  60. #60
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Do's and Don'ts So Others Won't

    A lot was made previously about using GoTo, but how do you handle errors without using Goto? Or is error handling the one exception?

  61. #61
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    Error handling is the exception - no real choice there.

  62. #62
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by romperstomper View Post
    Error handling is the exception - no real choice there.
    With the exception of GoTo 0, I have not used a GoTo in years. They can be completely avoided.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  63. #63
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    You mean you don't use error handling blocks, you just use On Error Resume Next?

  64. #64
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by romperstomper View Post
    You mean you don't use error handling blocks, you just use On Error Resume Next?
    Correct. In effect, error handling blocks are in line with the code. I will use Exit Sub in the block after setting appropraite error flags, if necessary.

    I know this is a style thing. I find it easier to assure myself that I have handed error conditions by leaving the hanlding block near the error.

    If VBA had try...catch like other languages, I would prefer it. On Error Resume Next the closest I can get.

  65. #65
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by blane245 View Post
    If VBA had try...catch like other languages, I would prefer it. On Error Resume Next the closest I can get.
    I would have said that on error goto label was closer to Try...Catch, but hey, it's your code.

    Of course that assumes you can predict every single error...

  66. #66
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by romperstomper View Post
    I would have said that on error goto label was closer to Try...Catch, but hey, it's your code.

    Of course that assumes you can predict every single error...
    You are right about goto label being close to try...catch. You got me on that one. I think the goto label method is definately a good way to protect code for any type of unforseen error, though a bit weak in knowing what to do when one of those unforseen errors occur. In general, doing exception handling is like writing a second program inside on the original one.

    Error prediction is an essential part of robust coding. If you are trying to protect against hardware as well as software faults, then there may no other way than to use on error goto label. But in the VBA environment, I am not sure your code will get control on those types of errors. For example, if you have a read failure on a disk drive, will VBA let your code get control of that, or will VBA or Windows trap that error and forget about you.

  67. #67
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    I agree that error prediction is an essential part of coding, but I don't believe it is possible to predict every error that might occur, so I think you should have some generic error handling, even if it's only in the top level sub.

    This is purely my opinion, and I've had some "lively" discussions in the past with various programmers (whom I otherwise respect) who disagree with me, so these days I just agree to differ on this point - as long as the error handling isn't simply putting On Error Resume Next at the top of every sub.

    As we all know, a fair proportion of coding practice is subjective (and the people on each side of any given argument seem to be equally vehement in general) so as long as I don't have to support other people's code I try to leave them to it. Unless of course, I think they are recommending bad practices to others...

  68. #68
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Do's and Don'ts So Others Won't

    Well stated. I am in the IV&V business and have done some study into how to determine if error handling is adequate. The research has not yet settled out to any clear means of making this determination. In the meantime, we are stuck with our own opinions.

  69. #69
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    I have no idea what IV&V is. I know what IV Guinness is, but I suspect that's quite different...

  70. #70
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by romperstomper View Post
    I have no idea what IV&V is. I know what IV Guinness is, but I suspect that's quite different...
    Don't feel bad - there are many who don't have a clue. On the other hand, I know (and love) Guinness, but never heard of IV Guinness.

  71. #71
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    Some drink Guinness in cans, some in bottles, others in pints. I take it IntraVenously.

  72. #72
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Do's and Don'ts So Others Won't

    Awesome, where can I get some?

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

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by romperstomper View Post
    I agree that error prediction is an essential part of coding, but I don't believe it is possible to predict every error that might occur, so I think you should have some generic error handling, even if it's only in the top level sub.
    So you us
    Please Login or Register  to view this content.
    for every sub? Dang, I've only used them with forms.

  74. #74
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by Mordred View Post
    So you us
    Please Login or Register  to view this content.
    for every sub? Dang, I've only used them with forms.
    On Error Goto label

    is better rather than ignore the error handle it!

    its also good to start with :-

    Please Login or Register  to view this content.

    I use this any time I use EnableEvents for speed, saves my hair when the code crashes,and I forget to manually turn events back on so then nothing works!, as Enable Events is permenent while ScreenUpdating only lasts until the end of the Sub/Function!

  75. #75
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Do's and Don'ts So Others Won't

    Quote Originally Posted by Mordred View Post
    So you us
    Please Login or Register  to view this content.
    for every sub? Dang, I've only used them with forms.
    Absolutely not. Other than quick and dirty code, I try to only use On Error Resume Next in short single-purpose routines (such as checking if a sheet exists) and use an error handler, rather than suppressor, in main routines (as Darren said).

  76. #76
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Do's and Don'ts So Others Won't

    What R. said! I will use multiple Resume Next/GoTo 0 segments if I am trying to trap multiple different errors in the ame routine, but never leave a Resume Next open for more than a single statement and its error handler.

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

    Re: Do's and Don'ts So Others Won't

    So then I do not have to go into all of my modules and the On Error Resume Next. Good to know. I know it is unlikely to catch all errors but I like to use If statements when I know that an error happens. In my own opinion, taking the time to test code in regards to the process it is to perform is the best way. It is time consuming (and something that most employers do not like) but it is an effective approach to code design.

  78. #78
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Do's and Don'ts So Others Won't

    FYI, this discussion is very educational for me. I've been teaching myself VBA (with the help of everyone here) and most of my brainpower has gone to wrapping my brain around the code. I'm just now starting to look at error handling. Thanks to all.

  79. #79
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Do's and Don'ts So Others Won't

    If it's an education for you I feel like I'm in primary school

    Dom

+ 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