VBA to Convert Column Number to Letter in Excel (3 Methods)

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.


VBA to Convert Column Number to Letter in Excel: 3 Methods

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.

VBA to Convert a Specific Column Number to Letter in Excel

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.

Result of VBA to Convert a Specific Column Number to Letter in Excel

Read More: 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

VBA to Convert Column Number to Letter with User-Defined Function (UDF) in Excel

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

VBA to Convert Column Number to Letter with User-Defined Function (UDF) in Excel with GoTo

  • 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.

Result of VBA to Convert Column Number to Letter with User-Defined Function (UDF) in Excel with GoTo

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

VBA to Convert Column Number to Letter with User-Defined Function (UDF) in Excel with Do-While

  • 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.

Result of VBA to Convert Column Number to Letter with User-Defined Function (UDF) in Excel with Do-While

Read More: How to Use VBA Range Based on Column Number in Excel


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.

VBA to Convert Column Number to Letter from User Input in Excel

  • 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).

Result of VBA to Convert Column Number to Letter from User Input in Excel

Read More: Column Letter to Number Converter in Excel


Download Workbook


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.


Related Articles


<< Go Back to Column Number | Columns in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo