简体   繁体   English

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

[英]How to get from Row, Column to Excel A1 notation?

Given a Row and Column (As Long), how can you determine the spreadsheet notation using VBA in Excel (2007):给定行和列(As Long),如何在 Excel (2007) 中使用 VBA 确定电子表格符号:

eg:例如:

(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.此主工作表控制工作表上的转换,但范围值显然采用 Excel 表示法,以便以后在引用范围时方便使用。 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

Maybe this is what you are looking for?也许就是您要找的?

Column Numbers to Letters 列号到字母

Column Letters to Numbers 列字母到数字

The good stuff is in the comments好东西在评论里

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). http://support.microsoft.com/kb/833402是 Microsoft 针对将数字转换为字母(从 1,1 转换为 A1 的棘手部分)的问题的解决方案。 This actually has the beuaty of working in other applications than Excel as it relies on basic VBA.这实际上具有在 Excel 以外的其他应用程序中工作的好处,因为它依赖于基本的 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

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.表达式“rngTemp.Address(False, False, , , .Cells(1, 1))”将以 A1 表示法显示范围 rngTemp 的地址,其中不包含表示绝对地址的 $s。 To get an absolute address, replace 'False, False' with ','.要获得绝对地址,请将“False, False”替换为“,”。

Here are two solutions.这里有两个解决方案。 One with elegant generality, another simple and direct aimed at the present implementation of Excel.一种具有优雅的通用性,另一种简单直接针对Excel的当前实现。 The first is limited only by the precision of the Integer or Long data type.第一个仅受 Integer 或 Long 数据类型的精度限制。 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.如果最大列数增加到超过 18278,即列引用从三个字母变为四个字母时,第二个将失败。 Both are pure VBA with no reliance on a feature peculiar to an MS Office application.两者都是纯 VBA,不依赖于 MS Office 应用程序特有的功能。

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.列引用被视为连续的一组给定位数的 base 26 数字,字母表用作数字 A=0,B=1,.. 等。首先有 26 个单字母列。 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.然后是 26^2 = 676 个双字母列,然后是 26^3 = 17576 个三字母列,总共 18278 个,其中只有 16384 个被 Excel 使用。

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

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

AAA1,...,XFD1 (703 to 16384, 15682 columns of 26^3 = 17576 possible with three letters) AAA1,...,XFD1(703 到 16384,15682 列 26^3 = 17576 可能用三个字母)

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.目前最大列数为 16384,因此代码将使用 Integer(上限 32767)代替 Long。 If you like you can error check that column parameter C is not out of range.如果您愿意,可以错误检查列参数 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

This second is simple ("Quick and Dirty") and will work with current Excel.第二个很简单(“Quick and Dirty”)并且适用于当前的 Excel。 It will require serious modification if the maximum number of columns exceeds 18278 when the column reference goes from 3 to 4 letters.当列引用从 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用正则表达式从 excel 或工作表 A1 符号中获取行和列 - Get row and column from excel or sheet A1 notation using regex 为每一行拖动后如何在Excel填充中保留A1表示法? - How to retain A1 Notation in Excel fill after being dragged down for every row? Excel宏查找列B中的最后一行,选择另一个单元格A1,将公式从A1拖动到B中的最后一行 - Excel Macro Find Last Row In Column B, Select another cell A1, drag formula from A1 to last row in B Google Spreadsheet / EXCEL]如何获取行中的最后一个值? 使用A1作为第一个单元格? - Google Spreadsheet/EXCEL] How do I get the last value in a Row? Using A1 as the first cell? 如何从Excel工作表中读取a1列并执行字符串匹配以在Java程序中定义变量 - How to read column a1 from an Excel sheet and perform string matching to define variables in a Java program Excel 在线办公脚本。 从工作表 2 复制范围 A1:J10 并将其粘贴到另一张工作表的 A 列的最后一行 - Excel online Office-Scripts . Copy Range A1:J10 from worksheet2 and paste it on last row of column A of another sheet 如何使用OleDB从97/03 Excel文档中获取A1单元格? - How can I get A1 cell from a 97/03 Excel document with OleDB? 如何下载带有 Gspread 的 Google Docs excel 工作表并在本地访问数据(A1 表示法)? - How to download a Google Docs excel sheet with a Gspread and access data locally (A1 notation)? 如何引用没有A1标记的单元格? - How to refer to cell without A1 notation? 1400个文件中的范围矩阵A1:Q38,并列为新工作簿第A1行中的单列,随后是A2行 - range matrix A1:Q38 from 1400 files & listing as single column in new workbook row A1 followed by row A2 etc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM