Let's say I have this string which represents a cell: A2
.
What should I do to covert it to coordinates: (2, 1)
?
You can use Column and Row properties of the Range object:
Range("A2").Row
Range("A2").Column
Examlpe:
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
Without VBA
Suppose, cell C2
contains string "A2"
.
Then
=INDIRECT(C2)
returns reference to A2
=ROW(INDIRECT(C2))
returns row number - 2
=COLUMN(INDIRECT(C2))
returns column number - 1
="(" & ROW(INDIRECT(C2)) & "; " & COLUMN(INDIRECT(C2)) & ")"
returns coordinates in format (x; y)
- (2; 1)
UPD:
If you're using UDF, change your parameter type from String
to Range
:
Function GetData(Cell As Range)
MsgBox "My row is " & Cell.Row
MsgBox "My column is " & Cell.Column
End Function
if you call this UDF from worksheet like this: =GetData(A2)
, msg box would pop-up:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.