VBA Column Letter from Column Number. Explained.
I needed to do the above quite a bit. I could not always understand the methods I googled, so i wrote a few of my own and tried to explain them ans some i googled in detail. I thought I would share my Fuctions and will do some speed tests for comparing the methods. All the codes are explained in as much detail as possible in the ‘Comments in the codes.
_ 1) Address Methods
These methods are based on Manipulating the string representing a cell Address, say $D$1, obtained using something of this form Cells(1, 4 ).Address. A few example codes are given for manipulating such a string to give the required column here
http://www.excelforum.com/developmen...ml#post4213969
_ .............................................
_ 2) Character CHR Methods.
Characters A B C ... etc can conveniently be referred to in VBA by Chr(65) Chr(66) Ch(67) ... etc .So with a bit of mathematics based on the column of interest a variable, n, of type whole number ( such as Long) can be calculated, which then using Chr(n) returns us the required Column Letter ( Or Letters )
I think the key to understanding the next few codes is putting into words how the column Letters are organised. For The first 26 it is simply A B C... etc. For every further 26 going to the right we add an extra letter ( At the left ) AA AB AC.... BA BB BC ... etc. ( At the left ) until we go past ZZ. Then we start again AAA AAB AAC.... etc until XFD ( column 16384 ) ( For Excel from XL 2007 )
Note the following codes may look complicated, but there could be advantages over the above methods which interact with the worksheet and so could cause speed issues when working with final codes.
http://www.excelforum.com/developmen...ml#post4213978
http://www.excelforum.com/developmen...ml#post4213980
_ ............................................
_ 3) “Recursion Function method”
I think I may have understood this. (After about week of thinking about it..). An interesting Example of recursion where the Function calling itself ( That is the definition of Recursion ) does this calling in the code line which actually finally gives the returning value for the Function. I am not sure if this explainable, and certainly not in ' comments as the comments would be rewritten every time the function repeats!!!
Maybe I try to explain in words, for the example of trying to get the column letters ABC for Column Number 731
Here is the simplified Code
A fuller code which may help along with my attemted explanation below is given here:![]()
Please Login or Register to view this content.
http://www.excelforum.com/developmen...ml#post4213887
So. In that code we look for the example of column Number 731. The working code bit as regards which gives a letter has been encounterered in the various _ 2) Character CHR Methods.
However in this code that is the only thing done. ( we do not here work out any Unint26 or Unint26x26 , that is to say we do not work out how many times a 26 or a 26 x 26 is in the Column number, lclm)![]()
Please Login or Register to view this content.
The trick of this code is how to get that bit above to work 3 times to get us in this case A then B then C
At line 60 in that code the Fuction “goes off” and does itself 3 times .
The Function then “Starts” effectivelly in total 4 times. The Debug Lines i used to produce the Tables in the above Link show how VBA then “stacks” The same variable names, as they are used in each run and held effectively in a register with different values for each run.
After the condition is no longer met for the code line 60 to go off again , then the program ends sequentially each function ( starting with the one last started.. ) , coming back to the code line where it went off, 3 times, and that code line is carried out 3 times sequentially, but IMPORTANTLY, at each “End” the variable ( lclm ) takes its value from the “stack” “row” for that “run” number. Hence The Letter A then B then C is added to the variable
I guess really going through the code carefully in Debug Mode ( F8 ) is the best way to understand. Unless anyone else has any better explanation.. ... or any other code variations.....
Bookmarks