Number to Letter

Number to Letter › Number to letter VBA

Number to letter VBA

Letters
VBA code for these numbers

 
Visual Basic EditorAlt + F11
Module1 (Code)General · NumToLetter
Immediate (Ctrl + G)

The Immediate window shows what each call returns. Type ? before a call to print its result.

In VBA, convert a number from 1 to 26 to a letter with Chr(64 + n): Chr(65) is "A" and Chr(90) is "Z." For Excel column letters, use Split(Cells(1, n).Address, "$")(1), which turns 28 into "AB" and 16,384 into "XFD."

Type numbers into the tool, pick A1Z26 or column letters, and it writes a ready-to-paste VBA module with a NumToLetter function and a Demo sub. The editor preview shows the function in Module1 and the Immediate window prints the result of each call.

How number to letter VBA calculator works?

To convert a number to a letter in VBA, the number to letter VBA calculator adds 64 to the number and reads the character with that code. The calculator splits your list of numbers, checks each one, converts 1–26 with Chr(64 + n), or builds column letters (27 = AA) in column mode, and writes runnable VBA code for the same numbers. 8 5 12 12 15 becomes HELLO.

  1. Read the numbers

    Spaces, commas, or new lines separate the numbers.

    8 5 12 12 15
  2. Pick the mode

    A1Z26 maps 1–26 to A–Z. Column mode maps any positive number to Excel column letters.

    A1Z26
  3. Run Chr(64 + n)

    Each number n becomes the character with code 64 + n (A = 65 in ASCII and Unicode).

    Chr(64 + 8) = "H"
  4. Write the code

    The VBA snippet and its output appear side by side. Tick lowercase to use code 96 + n.

    HELLO

Column mode uses the worksheet: Split(Cells(1, n).Address, "$")(1) returns the column letters for any n up to 16,384 in Excel 2007 and later.

Number to letter VBA formula

The number to letter VBA formula is a code point offset. letter = character(64 + n) for capitals and character(96 + n) for lowercase, for n from 1 to 26. Column letters use bijective base 26: take (n − 1) mod 26, then divide (n − 1) by 26, and repeat.

Formula

letter = Chr(64 + n)
lower = character(96 + n)
col(n): r = (n − 1) mod 26, n = ⌊(n − 1) ÷ 26⌋, repeat

Where

n
number from 1 to 26 (A1Z26) or any positive whole number (columns)
64
ASCII code of @, the character before A (65)
96
ASCII code of `, the character before a (97)
r
remainder 0–25, read as A–Z

Worked example

n = 28, column moder = (28 − 1) mod 26 = 1 → Bn = ⌊27 ÷ 26⌋ = 1 → r = 0 → AColLetter2(28) = "AB"

Number to letter VBA conversion chart

The number to letter VBA conversion chart lists all 26 A1Z26 numbers with the ASCII codes and the VBA expression. 1 = A (code 65), 13 = M (code 77), and 26 = Z (code 90).

nLetterCode (capital)Code (lowercase)VBA expression
1A6597Chr(65) = "A"
2B6698Chr(66) = "B"
3C6799Chr(67) = "C"
4D68100Chr(68) = "D"
5E69101Chr(69) = "E"
6F70102Chr(70) = "F"
7G71103Chr(71) = "G"
8H72104Chr(72) = "H"
9I73105Chr(73) = "I"
10J74106Chr(74) = "J"
11K75107Chr(75) = "K"
12L76108Chr(76) = "L"
13M77109Chr(77) = "M"
14N78110Chr(78) = "N"
15O79111Chr(79) = "O"
16P80112Chr(80) = "P"
17Q81113Chr(81) = "Q"
18R82114Chr(82) = "R"
19S83115Chr(83) = "S"
20T84116Chr(84) = "T"
21U85117Chr(85) = "U"
22V86118Chr(86) = "V"
23W87119Chr(87) = "W"
24X88120Chr(88) = "X"
25Y89121Chr(89) = "Y"
26Z90122Chr(90) = "Z"

Column numbers past 26

nColumn letters
27AA
28AB
52AZ
53BA
78BZ
702ZZ
703AAA
1,000ALL
16,384XFD

VBA number to letter functions

' A1Z26: 1 = A ... 26 = Z
Function NumToLetter(n As Long) As String
    If n >= 1 And n <= 26 Then NumToLetter = Chr(64 + n)
End Function

' Excel column letter: 1 = A, 27 = AA, 16384 = XFD
Function ColLetter(n As Long) As String
    ColLetter = Split(Cells(1, n).Address, "$")(1)
End Function

' Column letter without touching the worksheet (works in Word and Access too)
Function ColLetter2(ByVal n As Long) As String
    Dim r As Long
    Do While n > 0
        r = (n - 1) Mod 26
        ColLetter2 = Chr(65 + r) & ColLetter2
        n = (n - 1) \ 26
    Loop
End Function

Cells(1, n).Address returns "$AB$1"; splitting on "$" gives 3 parts, and part (1) is the letters. ColLetter2 uses the bijective base-26 loop instead, so it runs in any Office app.

How to add the function to Excel

  1. Press Alt + F11 to open the Visual Basic Editor.
  2. Click Insert › Module.
  3. Paste the code from the tool.
  4. Back in the sheet, type =NumToLetter(A1) in any cell.
  5. Save the file as .xlsm (macro-enabled), or the code is removed on save.

Letter to number in VBA

Use Asc(UCase(letter)) - 64 for a single letter and Range(col & "1").Column for column letters. Asc("C") - 64 returns 3, and Range("AB1").Column returns 28.

Frequently asked questions

How do I convert a number to a letter in VBA?

Use Chr(64 + n). Chr(64 + 1) returns "A" and Chr(64 + 26) returns "Z." Use Chr(96 + n) for lowercase.

How do I get a column letter from a column number in VBA?

Use Split(Cells(1, n).Address, "$")(1). For n = 28, it returns "AB."

Why does Chr(64 + 27) not return AA?

Chr returns 1 character. Character 91 is "[", so numbers above 26 need the column letter function, not Chr.

How do I convert a letter to a number in VBA?

Use Asc(UCase(x)) - 64 for 1 letter, or Range(x & "1").Column for column letters.

What is the VBA formula for number to letter?

The VBA formula is Chr(64 + n), so Chr(64 + 3) returns "C". Use Chr(96 + n) for lowercase.

What does Chr(65) return in VBA?

Chr(65) returns "A". Chr(90) returns "Z" and Chr(97) returns "a".

What is the largest column number in VBA?

16,384, which is column XFD in Excel 2007 and later. Excel 2003 stopped at column 256 (IV).