Range.Resize清除范围

时间:2018-08-21 17:14:55

标签: vba excel-vba

我正在尝试编写一个函数,该函数将填充一个范围,直到其长度与另一个范围相同。

Sub fill_down(ByRef drop_range As Range, ByRef same_length As Range)
    drop_range = drop_range.Resize same_length.Rows.Count
    With drop_range
        .FillDown
        Debug.Print .Resize(same_length.Rows.Count).Address
    End With
End Sub

Sub Run()
    Dim wsa As Worksheet
    Dim wsb As Worksheet
    Dim fill As Range
    Dim same_as As Range

    Set wsa = ActiveWorkbook.Sheets("Sheet1")
    Set wsb = ActiveWorkbook.Sheets("Sheet2")

    Set fill = wsa.Range("C3")
    Set same_as = wsb.Range("$K$14:$K$33")

    Call fill_down(fill, same_as)
End Sub

这将导致C3失去其公式,并且清除了整个范围。我在做什么错了?

1 个答案:

答案 0 :(得分:3)

以下工作吗?

Option Explicit

Sub Run()
    Dim wsa As Worksheet
    Dim wsb As Worksheet
    Dim fill As Range
    Dim same_as As Range

    Set wsa = ActiveWorkbook.Sheets("Sheet1")
    Set wsb = ActiveWorkbook.Sheets("Sheet2")

    Set fill = wsa.Range("C3")
    Set same_as = wsb.Range("$K$14:$K$33")

    fill_down fill, same_as
End Sub

Sub fill_down(ByRef drop_range As Range, ByRef same_length As Range)
    Set drop_range = drop_range.Resize(same_length.Rows.Count, 1)
    With drop_range
        .FillDown
        Debug.Print .Resize(same_length.Rows.Count).Address
    End With
End Sub