给定行和列(如长),如何在Excel(2007)中使用VBA确定电子表格符号:
e.g:
(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....
此主工作表控制工作表上的转换,但Range值显然是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
答案 0 :(得分:4)
也许this正是您要找的?
答案 1 :(得分:3)
答案 2 :(得分: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
答案 3 :(得分:0)
表达式'rngTemp.Address(False,False,,.Cells(1,1))'将以A1表示法显示rngTemp范围的地址,该地址不包含表示绝对地址的$ s。要获得绝对地址,请将“ False,False”替换为“,”。
答案 4 :(得分: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
这一秒很简单(“快速而肮脏”),并且可以在当前的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