我已经搜索过这个网站并用谷歌搜索了一个公式。我需要根据字母计算 Excel 列号,例如:
A=1 B=2 .. AA=27 AZ=52 ... AAA=703
The code seems to be 1 digit off after random cycles of the alphabet(AZ -> BA == off digit). It also will seemingly randomly produce the same integer from two different inputs:
GetColumnNumber(xlLetter : Text) : Integer //Start of function
StringLength := STRLEN(xlLetter);
FOR i := 1 TO StringLength DO BEGIN
Letter := xlLetter[i];
IF i>1 THEN
Count += ((xlLetter[i-1]-64) * (i-1) * 26) - 1;
Count += (Letter - 64);
END;
EXIT(Count); //return value
我的代码示例是用 C/AL 编写的,用于 Dynamics NAV,但我也可以编写 C# 或 vb.net,因此我不介意示例使用这两种语言中的任何一种。
最佳答案
在 VBA 中:
Public Function GetCol(c As String) As Long
Dim i As Long, t As Long
c = UCase(c)
For i = Len(c) To 1 Step -1
t = t + ((Asc(Mid(c, i, 1)) - 64) * (26 ^ (Len(c) - i)))
Next i
GetCol = t
End Function
关于excel - 如何从Excel列字母中获取列号(或索引),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17818358/