MS Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB ….. etc. In other words, column 1 is named as “A”, column 2 as “B”, column 27 as “AA”.

Given a column number, find its corresponding Excel column name. Following are more examples.

Input          Output
 26             Z
 51             AY
 52             AZ
 80             CB
 676            YZ
 702            ZZ
 705            AAC

Suppose we have a number n, let’s say 28. so corresponding to it we need to print the column name. We need to take remainder with 26.

If remainder with 26 comes out to be 0 (meaning 26, 52 and so on) then we put ‘Z’ in the output string and new n becomes n/26 -1 because here we are considering 26 to be ‘Z’ while in actual it’s 25th with respect to ‘A’.

Similarly if the remainder comes out to be non zero. (like 1, 2, 3 and so on) then we need to just insert the char accordingly in the string and do n = n/26.

Finally we reverse the string and print.

[ad type=”banner”]

Example:
n = 700

Remainder (n%26) is 24. So we put ‘X’ in output string and n becomes n/26 which is 26.

Remainder (26%26) is 0. So we put ‘Z’ in output string and n becomes n/26 -1 which is 0.

Following is python implementation of above approach.

[pastacode lang=”python” manual=”%23%20Python%20program%20to%20find%20Excel%20column%20name%20from%20a%20%0A%23%20given%20column%20number%0A%20%0AMAX%20%3D%2050%0A%20%0A%23%20Function%20to%20print%20Excel%20column%20name%20for%20a%20given%20column%20number%0Adef%20printString(n)%3A%0A%20%0A%20%20%20%20%23%20To%20store%20result%20(Excel%20column%20name)%0A%20%20%20%20string%20%3D%20%5B%22%5C0%22%5D*MAX%0A%20%0A%20%20%20%20%23%20To%20store%20current%20index%20in%20str%20which%20is%20result%0A%20%20%20%20i%20%3D%200%0A%20%0A%20%20%20%20while%20n%20%3E%200%3A%0A%20%20%20%20%20%20%20%20%23%20Find%20remainder%0A%20%20%20%20%20%20%20%20rem%20%3D%20n%2526%0A%20%0A%20%20%20%20%20%20%20%20%23%20if%20remainder%20is%200%2C%20then%20a%20’Z’%20must%20be%20there%20in%20output%0A%20%20%20%20%20%20%20%20if%20rem%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20string%5Bi%5D%20%3D%20’Z’%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%20(n%2F26)-1%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20string%5Bi%5D%20%3D%20chr((rem-1)%20%2B%20ord(‘A’))%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%20n%2F26%0A%20%20%20%20string%5Bi%5D%20%3D%20’%5C0’%0A%20%0A%20%20%20%20%23%20Reverse%20the%20string%20and%20print%20result%0A%20%20%20%20string%20%3D%20string%5B%3A%3A-1%5D%0A%20%20%20%20print%20%22%22.join(string)%0A%20%0A%23%20Driver%20program%20to%20test%20the%20above%20Function%0AprintString(26)%0AprintString(51)%0AprintString(52)%0AprintString(80)%0AprintString(676)%0AprintString(702)%0AprintString(705)” message=”Python Program” highlight=”” provider=”manual”/]

Output

Z
AY
AZ
CB
YZ
ZZ
AAC
[ad type=”banner”]