The ConvertToLetter function works by using the following algorithm:
- Divide the column number by 27, and then put the resulting integer in the variable "i".
- Subtract the column number from "i" multiplied by 26, and then put the result in the variable "j".
- Convert the integer values into their corresponding alphabetical characters, "i" and "j" will range from 0 to 26 respectively.
For example: The column number is 30.
- The column number is divided by 27: 30 / 27 = 1.1111,
rounded down by the Int function to "1".
i = 1 - Next Column number - (i * 26) = 30 -(1 * 26) = 30 - 26 = 4.
j = 4 - Convert the values to alphabetical characters separately,
i = 1 = "A"
j = 4 = "D" - Combined together, they form the column designator "AD".
The following VBA function is just one way to convert column
number values into their equivalent alphabetical characters:
Function ConvertToLetter(iCol)
Dim iAlpha
Dim iRemainder
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 If
End Function
Note This function only converts integers that are passed to it into
their equivalent alphanumeric text character. It does not change the appearance
of the column or the row headings on the physical worksheet.
Source: support.microsoft.com
Comments