+ Reply to Thread
Results 1 to 5 of 5

Problem understanding code in answers

  1. #1
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Problem understanding code in answers

    Hi All. This is a very general question but I think the answer would really help me get better at VBA. Often when I ask a question I get answer back with code that refers to variable as "i" (as in dim i as x or For i = x) or "j" (as in dim j as y or for j = y). This has been a real problem for me because I don't know what those letters refer to and it makes it hard to debug the code. While I was learning (and still am) I was taught to use variables that are more descriptive (like nRow or nCol or nEachStock). My question is, is there - or does anyone know - the taxonomy that goes with the alphabetic variables? I'd really like to learn it. Like does "i" always mean the same thing? Similarly with "j" or "r". Thanks in advance.
    Last edited by egavasrg; 12-11-2011 at 12:57 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,043

    Re: Problem understanding code in answers

    Hi egavasrg,

    When you "Dim" a variable using VBA it creates a spot in memory for this variable. Some programmers like to use short, one letter variables like i or j. I'd suggest you stick to variables like "LastRow" or "LastCol" or "RowCtr" so you can look at the code and understand it better. For the programmers who use i and j and k as variables, they are not as good at teaching and explaining what they are doing.

    I'd suggest you NOT use the single letter variable names and create variables that are meaningful to you and your code. You will find reading code you wrote months or years ago will make a lot more sense when you revisit it.

    Example - what would you rather read?

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    See http://www.ozgrid.com/VBA/variables.htm for more info about naming variables.
    Last edited by MarvinP; 12-11-2011 at 12:29 PM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

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

    Re: Problem understanding code in answers

    Stick with the system you were taught, and Marvin advises.

    These single alpha variables are for the really smart VBa operators, designed to confuse and impress us lesser mortals.

    Having said that you'll find that they usually represent variable integers or other numbers
    e.g.
    Dim n as Long could equally be Dim nRow as Long

    It is generally accepted that using Option Explicit is more efficient than not using it, the engine doesn't have to search for the type if you define it, the length of the variable name makes no difference in execution speed.
    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.

  4. #4
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Problem understanding code in answers

    Thank you! This is very helpful. I completely agree with your assessment. Descriptive variable are much easier to read. Thanks again.

  5. #5
    Registered User
    Join Date
    04-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Problem understanding code in answers

    Thanks Marcol. You and Marvin have really made this clear for a "lesser mortal" like me. Thanks again.

+ 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