Convert Column Position Numbers to Column Letters

  download code vba
Function ColumnLetter(rng As Range) As String
'Precondition:
'- range is a single cell, range or column 
'- range is Row is not allowed (returns the rownumber)
'Testcases:
'Debug.Print ColumnLetter(ActiveSheet.Range("b1"))     '>>B
'Debug.Print ColumnLetter(ActiveSheet.Range("bb1:bc1"))'>>BB
'Debug.Print ColumnLetter(ActiveSheet.Columns(2))      '>>B
'Debug.Print ColumnLetter(ActiveSheet.Columns(31))     '>>AE
'Debug.Print ColumnLetter(ActiveSheet.Columns(31))     '>>31
Dim strAddress As String
    strAddress = rng.Address
    ' Because .Address is $$, drop the first
    ' character and the characters after the column letter(s).
    ColumnLetter = Mid(strAddress, InStr(strAddress, "$") + 1, InStr(2, strAddress, "$") - 2)
    'remove : in case of input is column (address looks like $A:$A and will return A:)
    ColumnLetter = Replace(ColumnLetter, ":", "")
End Function