Cad*_*oux 8 excel vba coordinate-systems
给定行和列(如长),如何在Excel(2007)中使用VBA确定电子表格符号:
例如:
(R, C) = (1, 1) -> "A1"
(R, C) = (2, 1) -> "A2"
(R, C) = (2, 2) -> "B2"
Run Code Online (Sandbox Code Playgroud)
因此,如果你有一个功能:
Function CellRef(R As Long, C As Long) As String
Run Code Online (Sandbox Code Playgroud)
提供该功能,你可以做类似的事情:
Worksheet.Range(CellRef(R1, C1) + ":" + CellRef(R2, C2)).Copy
Run Code Online (Sandbox Code Playgroud)
一个小背景,如果这是一个错误的方法:这是我有一个主表,描述表中的其他工作表:
WorksheetName, Range etc....
Run Code Online (Sandbox Code Playgroud)
此主工作表控制工作表上的转换,但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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)