在Excel
表格的VBA编程中,非常实用的一个函数,可以把数字转换为对应的列名。
'*****************************************************************************
'将Excel中列数转换为列名(如27列--->AA列)
'参数:var 列数
'返回:列名 string
'*****************************************************************************
Public Function NumToLei(ByVal var As Integer) As String
Dim res As String
Dim remainder As Integer '余数
Dim quotient As Integer '商
remainder = var Mod 26
If remainder = 0 Then
var = var - 26
remainder = 26
End If
quotient = var \ 26
If quotient <> 0 Then
res = NumToLei(quotient)
End If
NumToLei = res & Chr(remainder + 65 - 1)
End Function