Implementing VBA macro is the most effective, quickest and safest method to run any operation in Excel. In this article, we will show you how to convert the column number to letter in Excel using the VBA.
Download Workbook
You can download the free practice Excel workbook from here.
3 Methods on Implementing VBA to Convert Column Number to Letter in Excel
In this section, you will learn how to convert a specific column number to letter, user-input column number to letter and create a UDF (User-Defined Function) to convert column number to letter in Excel with VBA.
1. VBA to Convert a Specific Column Number to Letter in Excel
Steps to convert a specific column number to a letter in Excel with VBA are given below.
Steps:
- Press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.
- In the pop-up code window, from the menu bar, click Insert -> Module.
- Copy the following code and paste it into the code window.
Sub ColNumToLetter()
Dim ColNumber As Long
Dim ColLetter As String
'Input Column Number
ColNumber = 200
'Convert To Column Letter
ColLetter = Split(Cells(1, ColNumber).Address, "$")(1)
'Display Result
MsgBox "Column " & ColNumber & " = Column " & ColLetter
End Sub
Your code is now ready to run.
This code will convert column number 200 to its associated letter address.
- Press F5 on your keyboard or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the small Play icon in the sub-menu bar to run the macro.
You will get a Microsoft Excel pop-up message box displaying the letter address (GR) of the column number (e.g. 200) that you input inside the code.
Read More: VBA to Use Range Based on Column Number in Excel (4 Methods)
Similar Readings
- [Fixed] Excel Column Numbers Instead of Letters (2 Solutions)
- How to Convert Column Letter to Number Chart in Excel (4 Ways)
- Find Value in Row and Return Column Number Using VBA in Excel
2. VBA to Switch Column Number to Letter with User-Defined Function (UDF) in Excel
Steps to switch the column number to letter with UDF in Excel with VBA are given below.
Steps:
- Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
- In the code window, copy the following code and paste it.
Public Function NumToLtr(ColNum)
NumToLtr = Split(Cells(1, ColNum).Address, "$")(1)
End Function
This is not a Sub Procedure for the VBA program to run, this is creating a User Defined Function (UDF). So, after writing the code, instead of clicking the Run button from the menu bar, click Save.
- Now go back to the worksheet of interest and write the function you just created with VBA code (Function NumToLtr in the first line of the code) and inside the parentheses of the NumToLtr function, pass the cell reference number that you want to convert to the letter (in our case, we pass Cell B5 inside the parentheses).
So our final formula refers,
=NumToLtr(B5)
- Press Enter.
You will get the associated letter address (A) of the column number (1) in your dataset.
- Now drag the row down by Fill Handle to apply the UDF to the rest of the cells to convert them into letters.
With The GoTo Statement
The above procedure can also be done with the GoTo Statement.
- The VBA code to convert column number to letter with the GoTo Statement is,
Public Function Num_Letter(ByVal iColNum As Integer) As String
Dim SLetter As String
   On Error GoTo iError
   Num_Letter = Left(Cells(1, iColNum).Address(False, False), _
                  Len(Cells(1, iColNum).Address(False, False)) - 1)
   Exit Function
iError:
   Call MsgBox(Err.Number & " - " & Err.Description)
End Function
- Now, as shown above, call the UDF in the dataset, pass the cell reference number as the argument, press Enter and drag the row to convert the column number to letter in Excel with the GoTo Statement in VBA.
With The Do-While Loop
Do-While Loop is another way to convert column number to letter in VBA Excel.
- The VBA code to convert column number to letter with the Do-While Loop is,
Function NumberToLetter(iCol As Long) As String
   Dim a As Long
   Dim b As Long
   a = iCol
   NumberToLetter = ""
   Do While iCol > 0
      a = Int((iCol - 1) / 26)
      b = (iCol - 1) Mod 26
      NumberToLetter = Chr(b + 65) & NumberToLetter
      iCol = a
   Loop
End Function
- Similarly, call the UDF in the dataset, pass the cell reference number as the argument, press Enter and drag the row to convert the column number to letter in Excel with the Do-While Loop in VBA.
Read More: Excel VBA: Set Range by Row and Column Number (3 Examples)
3. VBA to Change Column Number to Letter from User Input in Excel
Steps to change the column number to letter from the user input in Excel with VBA are given below.
Steps:
- Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
- In the code window, copy the following code and paste it.
Sub GetNumberFromUser()
    Dim ColNumber As Long
    Dim ColLetter As String
    'User Input: Column Number
    ColNumber = InputBox("Please Enter a Column Number")
    'Convert the user-entered Column Number To Column Letter
    ColLetter = Split(Cells(1, ColNumber).Address, "$")(1)
    'Display the Result
    MsgBox "Column Number: " & ColNumber & _
                        " = Column Letter: " & ColLetter
End Sub
Your code is now ready to run.
- Run the macro.
- A Microsoft Excel pop-up message box will appear asking for a column number. Write the Column Number that you want to convert as the letter inside the box, then press OK.
You will get the converted result of the column number (e.g. 200) that you input to the associated letter address (GR).
Read More: VBA to Use Range Based on Column Number in Excel (4 Methods)
Conclusion
This article showed you how to convert the number to letter in Excel with VBA. I hope this article has been very beneficial to you. Feel free to ask if you have any questions regarding the topic.