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.
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 RowCtrSee http://www.ozgrid.com/VBA/variables.htm for more info about naming variables.For i = 2 to k 'Do stuff here next i
Last edited by MarvinP; 12-11-2011 at 11:29 AM.
One test is worth a thousand opinions.
Click the * below to say thanks.
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.
Thank you! This is very helpful. I completely agree with your assessment. Descriptive variable are much easier to read. Thanks again.
Thanks Marcol. You and Marvin have really made this clear for a "lesser mortal" like me. Thanks again.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks