假设我有一个字符串代表一个单元格:A2
。
要将其转换为坐标:(2, 1)
,应该怎么做?
没有VBA
假设单元格C2
包含字符串"A2"
。
那么
=INDIRECT(C2)
返回引用A2
=ROW(INDIRECT(C2))
返回行号-2
=COLUMN(INDIRECT(C2))
返回列号-1
="(" & ROW(INDIRECT(C2)) & "; " & COLUMN(INDIRECT(C2)) & ")"
以格式(x; y)
-(2; 1)
返回坐标更新:
如果您使用UDF,请将参数类型从String
更改为Range
:
Function GetData(Cell As Range)
MsgBox "My row is " & Cell.Row
MsgBox "My column is " & Cell.Column
End Function
如果你在工作表中这样调用UDF: =GetData(A2)
,将会弹出消息框:
Range("A2").Row
Range("A2").Column
例子:
Sub test()
Dim x As String
x = "A2"
MsgBox GetRow(x) & " " & GetColumn(x)
End Sub
Function GetRow(Cell As String)
GetRow = Range(Cell).Row
End Function
Function GetColumn(Cell As String)
GetColumn = Range(Cell).Column
End Function
=GetData("A2")
可以正常工作(从电子表格单元格运行时返回2)。 - IoannisSub
函数? - Eyal
=offset(ref,rows,cols,width,height)
也可能非常有用。 - HannuFunction GetData(Cell As String)...
。你是想像用户定义的函数一样使用它(在单元格中=GetData(A2)
),并且想要定义A2
的坐标吗? - Dmitry Pavliv