1. 程式人生 > >vba 數字變為excel對應列的字母

vba 數字變為excel對應列的字母

Public Function GetColName(ByVal y As Integer) As String
       Dim z     As Integer
       Dim i     As Integer
       Dim n(25)     As String

       For i = 0 To 25
               n(i) = Chr(65 + i)
       Next

       If y <= 26 Then
         GetColName = n(y - 1)
         Exit Function
       End If
      
       z = y \ 26
      
       If y Mod 26 = 0 Then
         GetColName = n(z - 2)
       Else
         GetColName = n(z - 1)
       End If
     
       z = y Mod 26
       If z > 0 Then
             GetColName = GetColName & n(z - 1)
       Else
             GetColName = GetColName & "Z"
       End If
  End Function