11

给定行和列(As Long),如何在 Excel(2007)中使用 VBA 确定电子表格符号:

例如:

(R, C) = (1, 1) -> "A1"
(R, C) = (2, 1) -> "A2"
(R, C) = (2, 2) -> "B2"

因此,如果您有一个功能:

Function CellRef(R As Long, C As Long) As String

它提供了该功能,您可以执行以下操作:

Worksheet.Range(CellRef(R1, C1) + ":" + CellRef(R2, C2)).Copy

一点背景知识,以防这是错误的方法:这样做的目的是我有一个主表,它描述了表格中的其他工作表:

WorksheetName, Range etc....

此主工作表控制工作表上的转换,但范围值显然采用 Excel 表示法,以便以后在引用范围时使用。但是,管理此表、报告异常并确保一致性的例程确实从行和列中的其他工作表中获取信息,例如,它获取知道某事开始和结束的行和列。

这是我最终得到的功能:

Private Function CellRef(R As Long, C As Long) As String
    CellRef = vbNullString
    On Error GoTo HandleError:
    CellRef = Replace(Mid(Application.ConvertFormula("=R" & R & "C" & C, XlReferenceStyle.xlR1C1, XlReferenceStyle.xlA1), 2), "$", "")
    Exit Function
HandleError:
End Function
4

5 回答 5

4

也许就是你要找的?

于 2009-07-11T21:13:24.703 回答
3

列号到字母

列字母到数字

好东西在评论里

于 2009-07-12T13:46:06.983 回答
1

http://support.microsoft.com/kb/833402是 Microsoft 解决将数字转换为字母的问题(从 1,1 转换为 A1 的棘手部分)。这实际上具有在 Excel 以外的其他应用程序中工作的优点,因为它依赖于基本的 VBA。

然后你添加:

' Converts row and column index to Excel notation, ie (3, 2) to B3.
Private Function generateExcelNotation(row As Integer, column As Integer) As String
    ' error handling of your choice, I go for returning an empty string
    If (row < 1 Or column < 1) Then
        generateExcelNotation = ""
        Exit Function
    End If
    generateExcelNotation = ConvertToLetter(column) & row
End Function
于 2014-03-12T17:59:01.130 回答
0

表达式 'rngTemp.Address(False, False, , , .Cells(1, 1))' 将以 A1 表示法显示范围 rngTemp 的地址,其中不包含表示绝对地址的 $s。要获得绝对地址,请将 'False, False' 替换为 ','。

于 2019-01-18T06:57:01.343 回答
0

这里有两个解决方案。一个具有优雅的通用性,另一个简单直接针对 Excel 的当前实现。第一个仅受 Integer 或 Long 数据类型的精度限制。如果最大列数增加到超过 18278(列引用从三个字母变为四个字母的点),则第二个将失败。两者都是纯 VBA,不依赖于 MS Office 应用程序特有的功能。

列引用被视为给定位数的以 26 为基数的连续组,其中字母表用作数字 A=0、B=1、.. 等。首先有 26 个单字母列。然后 26^2 = 676 个双字母列,然后 26^3 = 17576 个三字母列,总共 18278 个,其中 Excel 只使用了 16384 个。

A1,B1,...,Z1 (1-26, 26 列)

AA1,....,ZZ1,(27 到 702, 26^2 = 676 列)

AAA1,...,XFD1 (703 到 16384, 15682 列 26^3 = 17576 可能有三个字母)

这是第一个解决方案。目前最大列数为 16384,因此代码将使用 Integer(上限 32767)代替 Long。如果您愿意,可以错误检查列参数 C 是否超出范围。

    '
    ' A "pure" implementation limited only by precision of the Long integer data type    
    '     
    '
    ' The first step is to find how many letters are needed.
    ' the second is to translate the column index into 0..(26^n) - 1  range
    ' Finally render that value as a base 26 number using alphabet for digits
    '


       Public Function CoordToA1Cell(ByVal R As Long, ByVal C As Long) As String
        Dim colRef As String
        Dim cwork As Long
        Dim n As Integer
        '
        Static e(0 To 6) As Long ' powers of 26
        Static s(0 To 6) As Long ' index ranges for number of letters needed

    If C <= 0 OR R <= 0 Then Exit Function

        ' initialize on first call
           If e(0) = 0 Then ' first call
              s(0) = 1
              e(0) = 1
              For n = 1 To UBound(s)
                e(n) = 26 * e(n - 1)
                s(n) = s(n - 1) + e(n)
              Next n
           End If

           cwork = C
           colRef = ""
        '
        ' step one: discover how many letters are needed
        '
           n = 1
           Do
              If C < s(n) Then
                 n = n - 1
                 Exit Do
              End If
              n = n + 1
           Loop
        ' step two: translate into 0..(26^n) - 1 interval
           cwork = cwork - s(n)
        '
        ' Step three: represent column index in base 26 using alphabet for digits
        '
           Do While n > 0
             colRef = colRef & Chr(65 + cwork \ e(n))
             cwork = cwork Mod e(n)
             n = n - 1
           Loop
        ' the final (or only) digit
           colRef = colRef & Chr(65 + cwork)

        CoordToA1Cell = colRef & R

        End Function

这第二个很简单(“Quick and Dirty”),适用于当前的 Excel。当列引用从 3 到 4 个字母时,如果最大列数超过 18278,则需要认真修改。'

Public Function CoordToA1CellQAD(ByVal R As Long, ByVal C As Long) As String
Dim colRef As String
Dim cwork As Long

If C <= 0 OR R <= 0 Then Exit Function

cwork = C

If cwork <= 26 Then
   colRef = Chr(64 + cwork)
ElseIf cwork <= 26 * 26 + 26 Then
   cwork = cwork - (26 + 1)
   colRef = Chr(65 + (cwork \ 26)) & Chr(65 + (cwork Mod 26))
'ElseIf cwork <= 26 * 26 * 26 + 26 * 26 + 26 Then  ' theoretical limit for three letters, 17576
ElseIf cwork <= 16384 Then                         ' actual Excel limit for columns
   cwork = cwork - (26 * 26 + 26 + 1)
   colRef = Chr(65 + (cwork \ 676))
   cwork = cwork Mod 676
   colRef = colRef & Chr(65 + (cwork \ 26)) & Chr(65 + (cwork Mod 26))
Else ' overflow
   Exit Function
End If

CoordToA1CellQAD = colRef & R

End Function
于 2020-04-26T21:50:04.520 回答