Excel 将列编号转换为字母的函数?

Excel 将列编号转换为字母的函数?,excel,vba,excel-udf,Excel,Vba,Excel Udf,是否有人有Excel VBA函数可以从数字返回列字母 例如,输入100应返回CV此函数返回给定列号的列字母 Function Col_Letter(lngCol As Long) As String Dim vArr vArr = Split(Cells(1, lngCol).Address(True, False), "$") Col_Letter = vArr(0) End Function 第100列的测试代码 如果不希望使用范围对象,请执行以下操作: Funct

是否有人有Excel VBA函数可以从数字返回列字母


例如,输入100应返回
CV

此函数返回给定列号的列字母

Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function
第100列的测试代码


如果不希望使用范围对象,请执行以下操作:

Function ColumnLetter(ColumnNumber As Long) As String
    Dim n As Long
    Dim c As Byte
    Dim s As String

    n = ColumnNumber
    Do
        c = ((n - 1) Mod 26)
        s = Chr(c + 65) & s
        n = (n - c) \ 26
    Loop While n > 0
    ColumnLetter = s
End Function

对我有效的是:

Cells(Row,Column).Address 

这将为您返回$AE$1格式参考。

和使用递归的解决方案:

Function ColumnNumberToLetter(iCol As Long) As String

    Dim lAlpha As Long
    Dim lRemainder As Long

    If iCol <= 26 Then
        ColumnNumberToLetter = Chr(iCol + 64)
    Else
        lRemainder = iCol Mod 26
        lAlpha = Int(iCol / 26)
        If lRemainder = 0 Then
            lRemainder = 26
            lAlpha = lAlpha - 1
        End If
        ColumnNumberToLetter = ColumnNumberToLetter(lAlpha) & Chr(lRemainder + 64)
    End If

End Function
函数ColumnNumberToLetter(iCol长度)为字符串
黯淡的拉尔法如长
昏暗的灯光和长的一样
如果iCol是优雅的,但要使其成为未来的证明,请将n的声明更改为long类型

如果您想要一个公式来避免宏,这里有一个适用于第702列(包括第702列)的公式

=IF(A1>26,CHAR(INT((A1-1)/26)+64),"")&CHAR(MOD(A1-1,26)+65)

其中A1是包含要转换为字母的列号的单元格。

这是基于上述的函数。如果你给我竖起大拇指,也给他竖起大拇指P

Function outColLetterFromNumber(iCol as Integer) as String
    sAddr = Cells(1, iCol).Address
    aSplit = Split(sAddr, "$")
    outColLetterFromNumber = aSplit(1)
End Function

还有一个办法。让我想到了这一点,但是如果你使用这个方法,你不必使用变量数组,你可以直接转到一个字符串

ColLtr = Cells(1, ColNum).Address(True, False)
ColLtr = Replace(ColLtr, "$1", "")
或者用这个可以使它更紧凑一点

ColLtr = Replace(Cells(1, ColNum).Address(True, False), "$1", "")

请注意,这确实取决于您引用cells对象中的第1行。

最新更新:请忽略下面的函数,@SurasinTancharoen设法提醒我,它在
n=53处被破坏
对于那些感兴趣的人,这里有其他的断开值,就在
n=200
下面:

更新结束


以下功能由Microsoft提供:

Function ConvertToLetter(iCol As Integer) As String
   Dim iAlpha As Integer
   Dim iRemainder As Integer
   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
资料来源:

适用于

  • Microsoft OfficeExcel 2007
  • Microsoft Excel 2002标准版
  • MicrosoftExcel2000标准版
  • Microsoft Excel 97标准版
    • 还有另一种方法:

      {
      
            Sub find_test2()
      
                  alpha_col = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,W,Z" 
                  MsgBox Split(alpha_col, ",")(ActiveCell.Column - 1) 
      
            End Sub
      
      }
      

      使用Excel power有一种非常简单的方法:使用
      Range.Cells.Address
      property,方法如下:

      strCol = Cells(1, lngRow).Address(xlRowRelative, xlColRelative)
      
      这将返回第1行上所需列的地址。以
      1
      为例:

      strCol = Left(strCol, len(strCol) - 1)
      
      请注意,它是如此快速和强大,以至于您可以返回甚至存在的列地址


      使用
      Selection.column
      属性将所需的列号替换为
      lngRow

      这里有一个简单的单内衬,可以使用

      ColumnLetter = Mid(Cells(Row, LastColA).Address, 2, 1)
      
      它只适用于1个字母的列名称,但对于简单的情况很好。如果您需要它专门用于2个字母的指定,那么您可以使用以下选项:

      ColumnLetter = Mid(Cells(Row, LastColA).Address, 2, 2)
      

      无论位于第X行、第Y列的单元格的一个代码行中有哪列,这都将起作用:

      Mid(Cells(X,Y).Address, 2, instr(2,Cells(X,Y).Address,"$")-2)
      
      如果您有一个具有唯一定义名称“Cellname”的单元格:


      获取列名的简单方法

      Sub column()
      
      cell=cells(1,1)
      column = Replace(cell.Address(False, False), cell.Row, "")
      msgbox column
      
      End Sub
      

      我希望它有帮助=)

      这可以通过使用以下公式获得:

      =SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")
      
      因此,也可以根据要求编写为VBA函数:

      Function ColName(colNum As Integer) As String
          ColName = Split(Worksheets(1).Cells(1, colNum).Address, "$")(1)
      End Function
      
      这是的一个版本(具有的风格),使用递归而不是循环

      Public Function ColumnLetter(Column As Integer) As String
          If Column < 1 Then Exit Function
          ColumnLetter = ColumnLetter(Int((Column - 1) / 26)) & Chr(((Column - 1) Mod 26) + Asc("A"))
      End Function
      

      brettdj的解决方案非常有效,但是如果你发现这是一个潜在的解决方案,原因与我相同,我想我会提供我的替代解决方案

      我遇到的问题是根据MATCH()函数的输出滚动到特定列。我没有将列编号转换为列字母平行,而是选择临时将引用样式从A1切换为R1C1。这样我就可以滚动到列号,而不必使用VBA函数。要在两种参考样式之间轻松切换,可以使用以下VBA代码:

      Sub toggle_reference_style()
      
      If Application.ReferenceStyle = xlR1C1 Then
        Application.ReferenceStyle = xlA1
      Else
        Application.ReferenceStyle = xlR1C1
      End If
      
      End Sub
      

      列编号中的列字母可以通过以下步骤使用公式提取
      1.使用地址公式计算列地址
      2.使用MID和FIND函数提取列字母

      示例:
      1.地址(10001000,1)
      结果$ALL$1000
      2. =中间(F15,2,FIND(“$”,F15,2)-2)
      结果所有asuming F15都包含步骤1的结果

      我们可以一次写
      MID(地址(10001000,1),2,FIND(“$”,地址(10001000,1),2)-2)

      Sub-GiveAddress()
      作为字符串的Dim Chara
      Chara=“”
      Dim Num作为整数
      淡色秋千一样长
      ColNum=InputBox(“输入列号”)
      做
      如果ColNum<27,则
      Chara=Chr(ColNum+64)&Chara
      退出Do
      其他的
      Num=ColNum/26
      如果(Num*26)>ColNum,则Num=Num-1
      如果(Num*26)=ColNum,那么Num=((ColNum-1)/26)-1
      Chara=Chr((ColNum-(26*Num))+64)和Chara
      ColNum=Num
      如果结束
      环
      MsgBox“Address is'&Chara&”“。”
      端接头
      
      关于brettdj答案的进一步说明,这里是使列号输入可选的。如果省略了列号输入,则函数返回调用该函数的单元格的列字母。我知道这也可以通过使用
      ColumnLetter(COLUMN())
      来实现,但我认为如果它能够聪明地理解这一点,那就太好了

      Public Function ColumnLetter(Optional ColumnNumber As Long = 0) As String
          If ColumnNumber = 0 Then
              ColumnLetter = Split(Application.Caller.Address(True, False, xlA1), "$")(0)
          Else
              ColumnLetter = Split(Cells(1, ColumnNumber).Address(True, False, xlA1), "$")(0)
          End If
      End Function
      

      这个函数的折衷是,由于
      IF
      测试,它会比brettdj的答案稍微慢一点。但是,如果该函数重复使用了大量时间,就会感觉到这一点。

      这仅适用于REFEDIT。。。通常使用维护代码 简短的版本。。。易于阅读和理解/ 它使用美元的poz

      Private Sub RefEdit1_Change()
      
          Me.Label1.Caption = NOtoLETTER(RefEdit1.Value) ' you may assign to a variable  var=....'
      
      End Sub
      
      Function NOtoLETTER(REFedit)
      
          Dim First As Long, Second As Long
      
          First = InStr(REFedit, "$")                 'first poz of $
          Second = InStr(First + 1, REFedit, "$")     'second poz of $
      
          NOtoLETTER = Mid(REFedit, First + 1, Second - First - 1)   'extract COLUMN LETTER
      
      End Function
      
      A章为65,因此:

      MsgBox-Chr(ActiveCell.Column+64)


      可以在中找到:

      这是一个迟来的答案,仅用于在1-3个字符列中使用
      Int()
      If
      的简化方法:

      Function outColLetterFromNumber(i As Integer) As String
      
          If i < 27 Then       'one-letter
              col = Chr(64 + i)
          ElseIf i < 677 Then  'two-letter
              col = Chr(64 + Int(i / 26)) & Chr(64 + i - (Int(i / 26) * 26))
          Else                 'three-letter
              col = Chr(64 + Int(i / 676)) & Chr(64 + Int(i - Int(i / 676) * 676) / 26)) & Chr(64 + i - (Int(i - Int(i / 676) * 676) / 26) * 26))
          End If
      
          outColLetterFromNumber = col
      
      End Function
      
      函数outColLetterFromNumber(i为整数)为字符串
      如果我<27,那么是一个字母
      col=Chr(64+i)
      ElseIf i<677然后是两个字母
      col=Chr(64+Int(i/26))和Chr(64+i-(Int(i/26)*26))
      埃尔斯的三封信
      col=Chr(64+Int(i/676))&Chr(64+Int(i-Int
      
      Sub GiveAddress()
          Dim Chara As String
          Chara = ""
          Dim Num As Integer
          Dim ColNum As Long
          ColNum = InputBox("Input the column number")
      
          Do
              If ColNum < 27 Then
                  Chara = Chr(ColNum + 64) & Chara
                  Exit Do
              Else
                  Num = ColNum / 26
                  If (Num * 26) > ColNum Then Num = Num - 1
                  If (Num * 26) = ColNum Then Num = ((ColNum - 1) / 26) - 1
                  Chara = Chr((ColNum - (26 * Num)) + 64) & Chara
                  ColNum = Num
              End If
          Loop
      
          MsgBox "Address is '" & Chara & "'."
      End Sub
      
      Public Function ColumnLetter(Optional ColumnNumber As Long = 0) As String
          If ColumnNumber = 0 Then
              ColumnLetter = Split(Application.Caller.Address(True, False, xlA1), "$")(0)
          Else
              ColumnLetter = Split(Cells(1, ColumnNumber).Address(True, False, xlA1), "$")(0)
          End If
      End Function
      
      Private Sub RefEdit1_Change()
      
          Me.Label1.Caption = NOtoLETTER(RefEdit1.Value) ' you may assign to a variable  var=....'
      
      End Sub
      
      Function NOtoLETTER(REFedit)
      
          Dim First As Long, Second As Long
      
          First = InStr(REFedit, "$")                 'first poz of $
          Second = InStr(First + 1, REFedit, "$")     'second poz of $
      
          NOtoLETTER = Mid(REFedit, First + 1, Second - First - 1)   'extract COLUMN LETTER
      
      End Function
      
      Function outColLetterFromNumber(i As Integer) As String
      
          If i < 27 Then       'one-letter
              col = Chr(64 + i)
          ElseIf i < 677 Then  'two-letter
              col = Chr(64 + Int(i / 26)) & Chr(64 + i - (Int(i / 26) * 26))
          Else                 'three-letter
              col = Chr(64 + Int(i / 676)) & Chr(64 + Int(i - Int(i / 676) * 676) / 26)) & Chr(64 + i - (Int(i - Int(i / 676) * 676) / 26) * 26))
          End If
      
          outColLetterFromNumber = col
      
      End Function
      
      Function fColLetter(iCol As Integer) As String
        On Error GoTo errLabel
        fColLetter = Split(Columns(lngCol).Address(, False), ":")(1)
        Exit Function
      errLabel:
        fColLetter = "%ERR%"
      End Function
      
      function GetColLetterFromNum(Sheet : Variant; Col : Integer) : String;
      begin
        Result := Sheet.Columns[Col].Address;  // from Col=100 --> '$CV:$CV'
        Result := Copy(Result, 2, Pos(':', Result) - 2);
      end;
      
      Function ColLetter(Col_Index As Long) As String
      
          Dim ColumnLetter As String
      
          'Prevent errors; if you get back a number when expecting a letter, 
          '    you know you did something wrong.
          If Col_Index <= 0 Or Col_Index >= 16384 Then
              ColLetter = 0
              Exit Function
          End If
      
          ColumnLetter = ThisWorkbook.Sheets(1).Cells(1, Col_Index).Address     'Address in $A$1 format
          ColumnLetter = Mid(ColumnLetter, 2, InStr(2, ColumnLetter, "$") - 2)  'Extracts just the letter
      
          ColLetter = ColumnLetter
      End Sub