+ Reply to Thread
Results 1 to 13 of 13

Seeking knowledge on an unknown VBA concept

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Lightbulb Seeking knowledge on an unknown VBA concept

    Disclaimer: "unknown" in Thread title means that I don't know the name of the concept I am about to describe. (Have not done a VBA course yet so please forgive what is probably a newbie question).


    Please see pseudo code below
    Please Login or Register  to view this content.
    Assume that the above two sketches are the same macro except that in the first I am declaring my Dims etc as soon as the macro starts and in the second I am declaring 'just in time' (so they are declared just before the code lines that require them).
    1. What is the name of the concept I have described above?
    2. What is the pros & cons of each method?
    3. Would the second method be faster as the memory doesn't have to hold the data when it doesn't need to?

    Appreciate any answers to the above!
    Last edited by mc84excel; 02-17-2013 at 10:47 PM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

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

    Re: Seeking knowledge on an unknown VBA concept

    What you're talking about is very close to the concept of late binding versus early binding.

    In your particular example the 2nd example won't be any faster than the first - when you declare a variable VB is just allocating the memory space it's going to use. Unless you were talking about thousands of variables it's not likely to make any noticeable difference at all, and certainly won't make up for the time you'll waste hunting through your code trying to find your variable declarations.

    A better example would be this code:

    Please Login or Register  to view this content.
    This is late-bound code and will work fine, but VBA doesn't know exactly what I want my two variables to hold up until the point where I assign them. This means that it has to reserve a big old chunk of memory and convert the variables to the correct type at the point of assignment.

    Also, if I make a mistake and instead of:

    Please Login or Register  to view this content.
    I accidentally code:

    Please Login or Register  to view this content.
    Then the compiler won't throw an error, it will just assume that I want Example1 to be a string, and treat it as such. It might not error until long after this assignation, when I actually try to treat Example1 as a range object instead of a string.

    So, this code could be re-written to early-bind my variables, thus:

    Please Login or Register  to view this content.
    Now VBA knows from the outset that Example1 is a range and Example2 is a worksheet, and sets them up as such from the start. It will throw an error if I try to treat either of them as something other than their assigned type.

    A Google for late binding versus early binding will give you a lot more information than I can.

    Does that help?

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Seeking knowledge on an unknown VBA concept

    I'd read your question slightly differently to Andrew, so here's my two pennyworth in addition to Andrew's eloquent description of late binding.

    Convention is to declare your variables at the top of the routine since VBA variables are scoped for the routine they run in. This actually makes little sense until you compare it to other languages, for example:

    VB.NET
    PHP Code: 
    Dim Test As Boolean

    Test 
    true

    If Test then
        Dim t 
    as long
        t 
    200
        Messagebox
    .show(t)
    End If

    't is now out of scope, so it is not declared 
    VBA
    Please Login or Register  to view this content.
    So whereas VB.Net has block level scope - t is only available in the If block, VBA does not, t is available throughout the routine.

    In VBA, it's therefore really a matter of preference. Keeping the variable declaration near the code that uses it makes sense from a refactoring viewpoint - it makes it easier to transfer the whole bit to another sub routine. Just bear in mind it can't be used before it's declared - so usually the preference is to declare at the top so it's usable throughout.

    In addition to Andrew's point, the only time declaring at the top may slow your code down is when you auto instance in your declaration, so:
    Please Login or Register  to view this content.
    As opposed to:
    Please Login or Register  to view this content.
    The reason for that is the class is created earlier - it's generally not good practice anyway since you can't check whether the class is nothing (which is often useful)

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

    Re: Seeking knowledge on an unknown VBA concept

    re the last point I don't believe that is correct-the class is not created at the dim line but rather the first time it is used as an object instance. I agree it is bad practice though :-)
    Josie

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

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Seeking knowledge on an unknown VBA concept

    @Joseph, I agree, which is why I said created earlier rather at declaration the act of testing if the object is nothing creates it. Thanks for the clarification though - should have been clearer

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Seeking knowledge on an unknown VBA concept

    Thank you Andrew-R and Kyle123. I have no doubt your explanations are correct but I feel they didn't address my original questions.

    I probably didn't explain myself clearly. Here's a new example:

    Please Login or Register  to view this content.
    Assume that all three subs do the exact same calculation and that only the layouts are different.

    Note that with the layouts:
    v1 = Dims at start of the sub and so are the values
    v2 = Dims at start of the sub but the values are not set until just before the string references are required.
    v3 = Dims and values are not set until just before the string references are required.

    1. Is there a name to the layout used in v2 & v3? (where everything is declared/set just before they are used) If so, what is it called?
    2. Would there be any advantage/disadvantage to each layout? (e.g. v1 is probably the easiest to read, but I am guessing v3 is the fastest? Or am I wrong?)
    3. Pretend that these subs and string values are very large - Wouldn't layout 3 be the fastest of them all - all other things being equal? (I am thinking of holding v1 holding the string values in memory all that time when they are not required).

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

    Re: Seeking knowledge on an unknown VBA concept

    OK, some general points first... There's a very definite limit to how large your subs can be. If I recall correctly each sub is limited to 64Kb of plain text, but this is rarely a problem if your code is well structured. I did help out somebody with a procedure that was too large recently, but it's really not usually an issue.

    This page shows how much memory is reserved by Excel for each variable type when it's declared. As you can see it's a pretty trivial amount, certainly on modern machines with many Gigabytes of usable memory.

    So, to answer your specific questions:

    1. Not that I know of, sorry. In manufacturing there's a principle called Just-In-Time, where you avoid holding stock by ordering components such that they will arrive at the factory exactly when they're needed for assembly, so I suppose you could call this JIT coding

    2. The difference between declaring variables early and late is likely to be non-existent. Given that you're talking a couple of bytes for each type you'd run out of space for the module before you used a significant amount of memory. The savings would be somewhere on par with those gained by only using single character variable names. That being the case I'd go with always declaring variables at the start of the code, because it makes the definitions easy to find.

    3. According to the link up there you can have very long strings; approximately 2 billion characters. According to my rough 'n' ready maths that's about 2Gb of memory space! If you were dealing with strings of that size then there probably would be an advantage to storing them in memory for as little time as possible. However, if you're trying to store strings of that size and manipulate them in memory you've got to think that there are bigger problems with your code than where variables are declared/assigned. For 'normal' sized strings the actual memory usage (1Kb or less) would be trivial compared to the amount of available memory, so any savings would be negligible.

    Which, I suppose, brings me to a final point - VBA isn't a fast language. It's a very convenient and relatively easy way to access the power of Excel, but if you're interested in shaving every possible millisecond off your run time then it's probably not the language to be working in.

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Seeking knowledge on an unknown VBA concept

    What Andrew says

    It's also worth noting that if you know your strings in advance it's probably worth using constants, not variables and declared with module scope if you are going to be using them in multiple places. Setting your strings to vbnullstring is also pointless, they will go out of scope at the end of the sub and be released anyway. If you're really keen on ultra-optimisation, then have a look here http://www.aivosto.com/vbtips/stringopt.html (but string optimization is really only an issue with extremely long strings or 10,000s of iterations; but if that matters to you, then you should probably be looking at another platform - as Andrew says VBA is sloooow.

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Seeking knowledge on an unknown VBA concept

    Quote Originally Posted by Kyle123 View Post
    if you know your strings in advance it's probably worth using constants, not variables and declared with module scope if you are going to be using them in multiple places.
    True. I only used strings for the example subs for the purpose of my original questions. (It didn't have to be strings).

    Quote Originally Posted by Kyle123 View Post
    Setting your strings to vbnullstring is also pointless, they will go out of scope at the end of the sub and be released anyway.
    Good point. (I guess I should have had a few more
    Please Login or Register  to view this content.
    after the vbnullstrings in my example).

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Seeking knowledge on an unknown VBA concept

    Thank you Andrew-R. That was the answer I was looking for.

    Quote Originally Posted by Andrew-R View Post
    VBA isn't a fast language. It's a very convenient and relatively easy way to access the power of Excel, but if you're interested in shaving every possible millisecond off your run time then it's probably not the language to be working in.
    I am not the type who insists on speed at all costs (sacrificing readability etc. along the way!) However since I discovered that macros could be sped up to several seconds quicker merely by learning a few better coding habits (e.g. WITH), I have been interested in learning how to avoid causing VBA to run slower than what it has to.

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Seeking knowledge on an unknown VBA concept

    Thanks for the feedback

    Macros can be optimised a lot, but I think you're barking up the wrong tree with this one these are pretty much all the tips you'd need http://www.cpearson.com/excel/optimize.htm. Anything like the link I posted is just ringing out any last slack in the application and is unlikely to have any noticeable difference for most people. Far better to ditch the selects and turn off screen updating

    re the setting to vbnullstrings, you don't really need to set anything to null/nothing when you're done with it since the variable will be released automatically when there are no more references to it (though there are some exceptions to this, they are not common for many users).

  12. #12
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Seeking knowledge on an unknown VBA concept

    Quote Originally Posted by Kyle123 View Post
    these are pretty much all the tips you'd need http://www.cpearson.com/excel/optimize.htm.
    Already aware of this page but thanks for the tip.

    Quote Originally Posted by Kyle123 View Post
    re the setting to vbnullstrings, you don't really need to set anything to null/nothing when you're done with it since the variable will be released automatically when there are no more references to it
    No! You mean vbnullstrings have no practical use then? Is there an article on this?

  13. #13
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Seeking knowledge on an unknown VBA concept

    vbNullStrings of course have a practical use, when you want to set a string to null; this doesn't however require doing at the end of a procedure.

    Consider:
    Please Login or Register  to view this content.
    Has the same effect as:
    Please Login or Register  to view this content.
    The vbnullstring has no practical use. When the sub completes, sMyString will no longer be in scope so the memory will be released.

    Another Example:
    Please Login or Register  to view this content.
    Here, since sMyString is a module/class level declaration it will not go out of scope until all the code has finished running. As other subs may reference the sMyString it can be useful to set it to null.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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