Return VBA 函数中单元格地址的范围?

Return a range from a cell's address in an VBA function?

我有以下函数来查找单元格的范围:

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 来卸载数组。

使用这个

Set starting_cell_string = Range(find_last_column())

而不是starting_cell_string = find_last_column()

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

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

我已经将 starting_cell_string 的 var 声明更改为 String 中的 String你的函数返回了什么。我添加了一个新的 Range 对象类型变量 (starting_cell_range) 到 Set 定义的单元格通过地址字符串。