3

我搜索了这个网站并用谷歌搜索了一个公式。我需要从字母中计算一个 Excel 列号,例如:

A=1
B=2
..
AA=27
AZ=52
...
AAA=703

在字母表的随机循环(AZ -> BA == off digit)之后,代码似乎减少了 1 位数。它似乎还会从两个不同的输入中随机生成相同的整数:

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

我的代码示例是用用于 Dynamics NAV 的 C/AL 编写的,但我也可以编写 C# 或 vb.net,因此我不介意示例是否使用这两种语言中的任何一种。

4

3 回答 3

3

在 VBA 中:

Function ColLetter(C As Integer) As String
  If C < 27 Then
    ColLetter = Chr(64 + C)
  Else
    ColLetter = ColLetter((C - 1) \ 26) & ColLetter((C - 1) Mod 26 + 1)
  End If
End Function
于 2013-07-23T18:59:22.710 回答
3

在 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
于 2013-07-23T18:58:11.283 回答
0

在 VBA 中:(递归函数)

    Function Get_Col_Number(strColName As String, dRunningNo As Integer) As Double
        Dim dCurrentColNo As Double
        Dim dMultipleValue As Double

        strColName = Ucase(strColName)

        If (dRunningNo <= 0) Then Get_Col_Number = 0:   Exit Function

        dCurrentColNo = ((Asc(Mid(strColName, dRunningNo, 1)) - Asc("A") + 1))
        dMultipleValue = 26 ^ (Len(strColName) - dRunningNo)

        Get_Col_Number = (dCurrentColNo * dMultipleValue) + Get_Col_Number(strColName, (dRunningNo - 1))
    End Function

如下使用此功能。

Sub Main()
    Dim StrGetNoForThisColumnName As String
    StrGetNoForThisColumnName = "Xfd"

    Msgbox "Final Result : " & Get_Col_Number(StrGetNoForThisColumnName, Len(StrGetNoForThisColumnName))
End Sub
于 2015-08-03T03:19:44.593 回答