2011-07-29 136 views
2

在Excel VBA細胞,我想這樣做:獲取從地址

Dim cellAddress As String, cell As Range 
Set cellAddress = "=Sheet1!A7" 
Set cell = GetCellFromAddress(cellAddress) 

我怎麼能以最好的方式實施「GetCellFromAddress」?我知道我可以解析cellAddress,但這似乎有點尷尬...

我使用Excel 2007,如果它很重要。

+0

看來我要重新寫的範圍內的功能?我真的不明白這一點。 –

回答

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

Set cell = Range(cellAddress) 

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

這將顯示文本一個消息:

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

玩得開心!

+1

+1('= Sheet'中的'='也是可選的) –

1

我認爲freerider的答案已經足夠好了,並且我贊成了。

如果你想看到他的解決方案實現爲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