excel - 如何从Excel列字母中获取列号(或索引)

标签 excel function character

我已经搜索过这个网站并用谷歌搜索了一个公式。我需要根据字母计算 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/

相关文章:

excel - 仅复制行的选定范围

excel - 如何将工作表的第一行连接成以逗号分隔的列表?

javascript - 关于 console.log 和 javascript 函数的初学者问题

perl - 转义 > 字符

Java,对象化文件中的唯一字符并通过字符对象的increment()方法计算出现次数

vba - Excel 宏 : Copy sheet without links - just data

java - 无法使用java从selenium webdriver中的Excel工作表中获取数值

c++ - 组成员函数都需要先隐式互斥锁?

c - C 梯形法则错误答案

c - 根据第一个字符对文本行进行排序 - C 编程