如何将行、列转换为Excel A1表示法?

发布于 2024-07-27 14:09:00 字数 991 浏览 2 评论 0原文

给定行和列(只要),如何在 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

Given a Row and Column (As Long), how can you determine the spreadsheet notation using VBA in Excel (2007):

e.g.:

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

Thus if you had a function:

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

which provided that functionality, you could do something like:

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

A little background, in case this is the wrong approach to be taking: The purpose of this is that I have a master sheet which describes other worksheets in a table:

WorksheetName, Range etc....

This master sheet controls transformations on the sheet, but the Range value is obviously in Excel notation for convenient later use in referencing the range. However a routine to manage this table, report exceptions and ensure consistency really gets things from other sheets in row and column, so for instance it gets a row and column where it knows something is starting and ending.

Here's the function I ended up with:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

远山浅 2024-08-03 14:09:00

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

http://support.microsoft.com/kb/833402 is a Microsoft solution for the problem of converting numbers to letters (the tricky part of the conversion from 1,1 to A1). This actually has the beuaty of working in other applications than Excel as it relies on basic VBA.

Then you add:

' 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
溇涏 2024-08-03 14:09:00

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

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

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

第二个很简单(“快速而肮脏”)并且可以与当前的 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

Here are two solutions. One with elegant generality, another simple and direct aimed at the present implementation of Excel. The first is limited only by the precision of the Integer or Long data type. The second will fail if the maximum number of columns were to increase beyond 18278, the point when column references go from three letters to four letters. Both are pure VBA with no reliance on a feature peculiar to an MS Office application.

The column references are viewed as consecutive sets of base 26 numbers of a given number of digits with the alphabet serving as digits A=0,B=1,.. etc.
First there are 26 single letter columns.
Then 26^2 = 676 double letter columns,
Then 26^3 = 17576 triple letter columns for a total of 18278 of which only 16384 are used by Excel.

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

AA1,....,ZZ1,(27 to 702, 26^2 = 676 columns)

AAA1,...,XFD1 (703 to 16384, 15682 columns of 26^3 = 17576 possible with three letters)

This is the first solution.
Currently the maximum column number is 16384 so code will work with Integer (upper limit 32767) in place of Long.
If you like you can error check that column parameter C is not out of range.

    '
    ' 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

This second is simple ("Quick and Dirty") and will work with current Excel. It will require serious modification if the maximum number of columns exceeds 18278 when the column reference goes from 3 to 4 letters.
'

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
欢你一世 2024-08-03 14:09:00

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

The expression 'rngTemp.Address(False, False, , , .Cells(1, 1))' will display the address of the range rngTemp in A1 notation which does not contain $s to signify an absolute address. To get an absolute address, replace 'False, False' with ','.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文