+ Reply to Thread
Results 1 to 5 of 5

Thread: Problem understanding code in answers

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

    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 11:57 AM.

  2. #2
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    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?

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    For RowCtr = 2 to LastRow
    'Do Stuff here
    Next RowCtr
    For i = 2 to k
    'Do stuff here
    next i
    See http://www.ozgrid.com/VBA/variables.htm for more info about naming variables.
    Last edited by MarvinP; 12-11-2011 at 11:29 AM.
    One test is worth a thousand opinions.
    Click the * below to say thanks.

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

    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 click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

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

    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
    42

    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.2.0