开发者

Get a cell from the address

开发者 https://www.devze.com 2023-03-23 23:45 出处:网络
In Excel VBA, I would like to do this: Dim cellAddress As String, cell As Range Se开发者_如何学Ct cellAddress = \"=Sheet1!A7\"

In Excel VBA, I would like to do this:

Dim cellAddress As String, cell As Range
Se开发者_如何学Ct cellAddress = "=Sheet1!A7"
Set cell = GetCellFromAddress(cellAddress)

How could I implement "GetCellFromAddress" in the best way? I know I could parse the cellAddress, but that seems a little awkward...

I am using Excel 2007, if it matters.


Dim cellAddress As String, cell As Range
cellAddress = "=Sheet1!A7"

Set cell = Range(cellAddress)

MsgBox cell.Address(True, True, xlR1C1, True)

This will show a messagebox with text:

---------------------------
Microsoft Excel
---------------------------
[Book1]Sheet1!R7C1
---------------------------
OK   
---------------------------

Have fun!


I think freerider's answer is good enough and I upvoted it.

If you want to see his solution implemented as GetCellFromAddress

Function GetCellFromAddress(cellAddress As String) As Range
    Set GetCellFromAddress = Range(cellAddress)
End Function

Sub test()
    Dim cellAddress As String, cell As Range
    cellAddress = "=Sheet1!A7"

    Set cell = GetCellFromAddress(cellAddress)
    MsgBox cell.Address(True, True, xlR1C1, True)
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消