Excel VBA将单元格名称转换为它的坐标(Excel VBA Convert Cell Name to It's Coordinates)

编程入门 行业动态 更新时间:2024-10-25 02:19:29
Excel VBA将单元格名称转换为它的坐标(Excel VBA Convert Cell Name to It's Coordinates)

假设我有一个表示单元格的字符串: A2 。 如何将其转换为坐标: (2, 1) ?

Let's say I have this string which represents a cell: A2. What should I do to covert it to coordinates: (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)返回坐标

在此处输入图像描述

UPD:

如果您使用的是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) ,将弹出msg框:

“我的排是2” “我的专栏是1”

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)

enter image description here

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:

"My row is 2" "My column is 1"

更多推荐

本文发布于:2023-07-23 18:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   坐标   单元格   名称   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!