+ Reply to Thread
Results 1 to 8 of 8

Understanding DIM

Hybrid View

  1. #1
    Registered User
    Join Date
    02-14-2017
    Location
    Hilversum, Netherlands
    MS-Off Ver
    2010
    Posts
    92

    Understanding DIM

    Hi guys!

    I have a question about Variables and dimming

    I understand that it is best practice to dim as Excel otherwise has to guess what type of variable it is. Can someone however give me an example in which case this happens so I can make it more tangible for myself and my colleague?

    So an example in which a macro isn't working properly because the user has not dimmed his/her variable

    Thank you all in advance!

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Understanding DIM

    You can not have a variable with out dim in VBA. VBA is statically and strongly typed programming language, unlike dynamic languages, such as Python. You need to declare not only the name, but the data type as well. If you do not declare the data type, it assumed as a "Variant" data type, so if you say Dim MyVariable with out explicitly defined the data type, it assumed as "Variant" data type.
    Last edited by AB33; 09-28-2017 at 07:45 AM.

  3. #3
    Registered User
    Join Date
    02-14-2017
    Location
    Hilversum, Netherlands
    MS-Off Ver
    2010
    Posts
    92

    Re: Understanding DIM

    Thank you for your reply!

    Yes I understand that, I am asking for an example so that my colleague who never uses dim understands why it is dangarous to not declare

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Understanding DIM

    Let's say

    Dim i As Long, j,k
    Many people fallin for the above trap, assuming that all variables are declared as LONG, which is what you do in other c based programming languages. However, in VBA, this means, it is only (I) is declared as LONG, but , j and k as "Variant".

    In general, you should avoid declaring variables as "Variant" and be explicit in declaring data types. If you declare all variables as "Variant" , you have left for VBA to figure out how to fit in (Convert) each variables in to correct data type. This could result in overhead and may lead not only to poor performance, but also wrong output.
    Last edited by AB33; 09-28-2017 at 07:57 AM.

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Understanding DIM

    Another detail is this:

    using Option Explicit at the top of a sub will force you to Dim any variable used. That can be annoying, but also prevents problems. For example,

    Option Explicit
    
    Sub Calculate_Value
    
    Dim myValue1 as Long
    Dim myValue2 as Long
    
    myValue1 = 50
    myValue2 = myValu1 * 10 'notice typo in the myValue1 variable
    
    End Sub
    The code above will not run, letting you know myValu1 is not declared. If the programmer didn't notice the typo, and didn't use Option Explicit, then myValu1 not having been assigned a value, myValue2 would actually be 0 instead of the intended result of 500.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Understanding DIM

    why it is dangarous to not declare
    The ongoing discussion.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Understanding DIM

    If a macro is properly written, it would be extremely unlikely that not declaring a variable would stop it working. It might run slower, but it is unlikely to actually break.

    However, declaring variables can assist you while writing and debugging code, not just for spelling errors, but also catching issues where you forgot to use Set with a range object for example, or tried to pass the wrong data type to a routine, or merely providing Intellisense as you work.

    Here's a simple example where forgetting to use Set would result in no error at run time, but nothing would actually happen
    dataRange = range("A1:A100")
    ' do some stuff with an array
    dataRange = some_array
    You might have intended to output the array to the range in question but by omitting set all you did was assign one value to a variable then later assign another value to the variable. No complaints from the compiler at run time and therefore harder to track down.
    Last edited by xlnitwit; 09-28-2017 at 10:04 AM.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,457

    Re: Understanding DIM

    I agree with xlnitwit, declaring the type of variable is useful but bad coding is bad coding.

    Here is an example where variant type can cause confusion.

    Sub Test()
    
        Dim value1
        Dim value2
        Dim lngvalue1 As Long
        Dim lngvalue2 As Long
        
        
        value1 = Right("Name1", 1)
        value2 = Right("Name2", 1)
        
        Debug.Print value1 + value2
        
        lngvalue1 = Right("Name1", 1)
        lngvalue2 = Right("Name2", 1)
        
        Debug.Print lngvalue1 + lngvalue2
        
    End Sub
    When the variable type is defined the compiler can cast the return value with the appropriate type. Although use of Clng() function would make it explicit what the coder had in mind.

    A much more useful benefit of declaring variable type, especially objects, is intellisense
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Need help understanding
    By Robert1960 in forum Excel General
    Replies: 7
    Last Post: 06-14-2016, 11:56 AM
  2. Help need understanding footers
    By gigahacker in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-15-2013, 08:36 PM
  3. [SOLVED] Understanding This loop
    By blackrock41 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-16-2013, 12:21 AM
  4. understanding code
    By karinos57 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-12-2009, 08:35 PM
  5. Understanding ScreenUpdating
    By additude in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-08-2008, 10:40 AM
  6. Not understanding with/end with
    By davegb in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-06-2006, 05:45 PM
  7. Not understanding If Not..Then nothing
    By davegb in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-14-2005, 12:05 PM

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