首页 文章

在最后使用的列的右侧填充公式

提问于
浏览
2

我是VBA的新手 . 我试着编写一个代码,将填充单元格D3和D4中的公式向右和最后一列使用减去3.这就是我提出的但我不认为它非常正确

Sub fillRight()

Dim lastColumn As Integer

lastColumn = ActiveSheet.Cells(2, Columns.count).End(xlToLeft).Column - 3

    range("D3:D4").Select
    Selection.AutoFill Destination:=range("D3", lastColumn), Type:=xlFillDefault
    range("D3", lastColumn).Select

End Sub

任何帮助表示赞赏!

谢谢

1 回答

  • 1

    这个怎么样?

    Sub fillRight()
        Dim lastColumn As Integer
        Dim rng_source As Range
        Dim rng_Destination As Range
        Dim l_SourceRows As Long
    
        Set rng_source = Range("D3:D4")
        l_SourceRows = rng_source.Rows.Count
        lastColumn = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column - 3
        Set rng_Destination = Range(rng_source.Cells(1), Cells(rng_source.Cells(1).Row + l_SourceRows - 1, lastColumn))
    
        rng_source.AutoFill _
            Destination:=rng_Destination, _
            Type:=xlFillDefault
    End Sub
    

    通过设置最后一列,您在正确的路径上,但您还需要知道最后一行,以便为填充范围的最后一个单元格创建范围 .

相关问题