How to convert Excel column numbers into alphabetical characters
In Access I write data into an Excel file and need to address cells. For example I want to address the 50 th column and need to get the column address (in this example AX).
Function ConvertToLetter(iCol As Integer) As String Dim iAlpha As Integer Dim iRemainder As Integer iAlpha = Int(iCol / 27) iRemainder = iCol - (iAlpha * 26) If iAlpha > 0 Then ConvertToLetter = Chr(iAlpha + 64) End If If iRemainder > 0 Then ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64) End IfEnd Function
of course you can reference cells using numbers
excamle
activesheet.cells(1,50) ' where 1 is row number and 50 is column number
also you use extract column part from this this
ActiveSheet.Cells(1, 50).Address
Rgonzo1971
Of course Farwest answer is right
just to answer the question
Function ColumnLetter(ColNumber As Long) As String Dim lVar As Long Dim bVar As Byte Dim sVar As String lVar = ColNumber Do bVar = ((lVar - 1) Mod 26) sVar = Chr(bVar + 65) & sVar lVar = (lVar - bVar) \ 26 Loop While lVar > 0 ColumnLetter = sVarEnd Function
Public Function GetColumnName(colindex As Integer) As StringGetColumnName = Split(Cells(1, colindex).Address, "$")(1)End FunctionPublic Sub test()Debug.Print GetColumnName(10)End Sub
you can also use this single line of code if your column index will not exceed 701, that is not depend on worksheet reference or open sheet, and only if your column index will not exceed 701
Public Function GetColumnName(x As Integer) As StringGetColumnName = IIf(Int(x / 26) < 1, Chr(IIf(Int(x / 26) < 1, x, 1) + 64), Chr(Int(x / 26) + 64) + Chr((x - (Int(x / 26)) * 26) + 64))End Function
excamle
activesheet.cells(1,50) ' where 1 is row number and 50 is column number
also you use extract column part from this this
ActiveSheet.Cells(1, 50).Address