从VBA函数中的单元格地址返回范围?

时间:2015-03-14 23:24:26

标签: excel vba function range

我有以下功能来查找单元格的范围:

Function find_last_column()
    Dim ws As Worksheet
    Dim rng1 As Range
    Set ws = Sheets("Data_History")
    Set rng1 = ws.rows(1).Find("*", ws.[a1], xlFormulas, , xlByColumns, xlPrevious)
    find_last_column = rng1.Address
End Function

我想在以下子句中使用rng1.Address作为范围:

Sub Start_of_range()
    Dim starting_cell_string As Range
    starting_cell_string = find_last_column()
End Sub

我想使用来自`starting_cell_range'的Offset然后卸载一个数组。

2 个答案:

答案 0 :(得分:0)

使用此

Set starting_cell_string = Range(find_last_column())

而不是starting_cell_string = find_last_column()

答案 1 :(得分:0)

您似乎正在混合字符串地址和范围对象。

Function find_last_column()
    Dim ws As Worksheet
    Dim rng1 As Range
    Set ws = Sheets("Data_History")
    Set rng1 = ws.rows(1).Find("*", ws.[a1], xlFormulas, , xlByColumns, xlPrevious)
    'might as well include the worksheet reference for added functionality (external:=true)
    find_last_column = rng1.Address(1, 1, xlA1, external:=true)
    set rng1 = nothing
    set ws = nothing
End Function

Sub Start_of_range()
    Dim starting_cell_string As STRING, starting_cell_range As RANGE
    starting_cell_string = find_last_column()
    set starting_cell_range = range(starting_cell_string)

    ' do stuff here

    set starting_cell_range = nothing
End Sub

我已将Start_of_range子句中的 starting_cell_string 的var声明更改为 String ,这是您的函数返回的内容。我已将{em> Range 对象类型变量( starting_cell_range )添加到Set到由地址字符串定义的单元格。