无法将单元格区域的地址从一个函数返回到另一个函数,我得到“需要对象”错误

时间:2014-01-31 15:31:32

标签: excel vba object

当我尝试将范围的值从一个函数返回到另一个函数时,我收到“需要对象”错误。我阅读了有关“需要对象”的其他答案,但我无法弄清楚它与我的代码有什么关系。请原谅我,但我是VBA的新手。这是我的代码:

Function functionOne()
    Dim r As Range
    Workbooks("workbook1.xls").Activate
    Sheets("sheet1").Select
    Set r = GetRange("A1")
    Application.Goto r
End Function

Function GetRange(strStart As String) As Variant
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Set rng1 = Cells.Find("*", [A1], , , xlByRows, xlPrevious)
    Set rng2 = Cells.Find("*", [A1], , , xlByColumns, xlPrevious)
    Set rng3 = Range([strStart], Cells(rng1.Row, rng2.Column))
    GetRange = rng3.Value
End Function

'object required'错误发生在functionOne()的Set r = GetRange("A1")。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

GetRange功能更改为返回Range对象:

Function GetRange(strStart As String) As Range
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Set rng1 = Cells.Find("*", [A1], , , xlByRows, xlPrevious)
    Set rng2 = Cells.Find("*", [A1], , , xlByColumns, xlPrevious)
    Set rng3 = Range([strStart], Cells(rng1.Row, rng2.Column))
    Set GetRange = rng3
End Function