I found several chunks of code on the internet which were supposed to convert a 0-based column number in Excel to a letter, but I could get none of them to work with a value above 25. So here's the code I got working, for everyone's use. Enjoy.
###################################################################### # 7/25/2016 # In: Column index, 0-n. # Out: Letter(s) representing column in Excel. 0=A, 1=B, 25=Z, 26=AA.. +. # # Inspired by http://stackoverflow.com/questions/3302857/algorithm-to- +get-the-excel-like-column-name-of-a-number # 7/25/2016 WORKS! sub excelpos2ltr {my($oldcol)=@_; # Col starts at 0='A' my($procname,$numeric,$ltr,$num2,$outs); $procname="excelpos2ltr"; # Alt method 4. $numeric = $oldcol % 26; $ltr = chr(65 + $numeric); #$num2 = intval($oldcol / 26); # old line $num2 = int($oldcol / 26); if ($num2 > 0) { $outs=excelpos2ltr($num2 - 1) . $ltr; } else { $outs=$ltr; } return $outs; }
If you found any problems please put the code fix as a comment below.
In reply to Get excel column letter from number by bulrush
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |