VBA Single-Lettered Variables

Vathana

New Member
Joined
Jun 5, 2020
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hello all.

Weird question? But is it safe to name variables with one letter only (A to Z)? I've seen "I" and "J" often in examples on the webs and used those myself too, but I'm afraid ones of the letters are VBA keywords.

Thanks.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
no problem I am aware of using the single, often use n for counts myself
 
Upvote 0
It's quite safe. There are no 1 letter keywords in VBA.
 
Upvote 0
Whilst I do tend to use single letter variables for loop counters, I would generally advise against use them.
The main reason reason being it can be very difficult to find them. For instance if you have a variable called e, try doing a search for e & you will get a lot of responses for Range, resize, offset, value etc etc.
 
Upvote 0
Unless you use the 'find whole word only' option. :)
 
Upvote 0
Never even new that existed. :oops:
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
The first computer language I learned was FORTRAN. In FORTRAN, there is a design feature where any variable starting with the letters I through N is an integer, and variables starting with anything else are floating point. So I and J were often used as loop counters. The next language I learned was BASIC, and the convention carried over for me. I try to avoid a lower case L though, since it's easily confused with a 1 or an i. I keep thinking, like Fluff, that an actual name like Counter1, or RowCounter, would be better, but habits are hard to break. I do use r for row and c for column sometimes. But I see a lot of other code floating around where i and j are used as loop counters, and I've wondered how that happened. I rather doubt that my FORTRAN experience applies to that many people.
 
Upvote 0
The first computer language I learned was FORTRAN. In FORTRAN, there is a design feature where any variable starting with the letters I through N is an integer, and variables starting with anything else are floating point. So I and J were often used as loop counters. The next language I learned was BASIC, and the convention carried over for me. I try to avoid a lower case L though, since it's easily confused with a 1 or an i. I keep thinking, like Fluff, that an actual name like Counter1, or RowCounter, would be better, but habits are hard to break. I do use r for row and c for column sometimes. But I see a lot of other code floating around where i and j are used as loop counters, and I've wondered how that happened. I rather doubt that my FORTRAN experience applies to that many people.
You solved one the great mysteries of programming (at least for me)! I often seen people use "i and j" for loop counters, so much so that I started using it myself. But I never knew why or how that came about (never really thought much about it before now, to be honest). It is interesting to hear the genesis of that.

I never programmed in Fortran. The first language I learned in school was BASIC.
 
Upvote 0

Forum statistics

Threads
1,223,649
Messages
6,173,580
Members
452,521
Latest member
bdough27

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top