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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Get excel column letter from number
by MidLifeXis (Monsignor) on Jul 26, 2016 at 19:08 UTC | |
|
Re: Get excel column letter from number
by Corion (Patriarch) on Jul 26, 2016 at 11:09 UTC | |
|
Re: Get excel column letter from number
by haukex (Archbishop) on Jul 26, 2016 at 11:10 UTC | |
|
Re: Get excel column letter from number
by Anonymous Monk on Jul 26, 2016 at 11:12 UTC | |
by duelafn (Parson) on Jul 26, 2016 at 13:10 UTC | |
by pryrt (Abbot) on Jul 26, 2016 at 13:32 UTC | |
|
Re: Get excel column letter from number
by dasgar (Priest) on Jul 26, 2016 at 16:54 UTC |