在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)
我认为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