2016-11-17 51 views
1

我一直在使用這種語法爲D列添加一行總數 - 但現在我需要爲D-M添加總行數,而不是隻多次寫這些代碼並更改列名是否有一個快速可重用的函數,可以將總行添加到列D - M的底部?添加列的總行數D - M

With ActiveSheet 
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then 
    lastrow = .Cells.Find(What:="*", _ 
        After:=.Range("D1"), _ 
        Lookat:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        SearchOrder:=xlByRows, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Row 
Else 
    lastrow = 1 
End If 
End With 
Range("D" & lastrow +1).FormulaR1C1 = "=SUM(R[-" & lastrow & "]C:R[-1]C)" 

回答

3

這與突出顯示範圍,按Ctrl +右箭頭

同樣的效果
range("D" & lastrow + 1 & ":M" & lastrow + 1).FillRight 
+1

優秀,得到了我的答案,也學到了新的VBA函數。謝謝! –