使用带公式的值填充所有行

时间:2014-05-01 01:15:37

标签: excel vba finance

我希望能够获得有关此代码的最后一次调整的帮助。它可以通过两个额外的手动步骤工作得很好,但我希望通过宏自动完成。在最后一段中,我希望将一个公式包含在尽可能多的行中,因为行中包含值,而不是直到第244行。

Sub GLMacro2()
' Shortcut Ctrl+Shift+H
    Range("A1").Select
    ActiveCell.SpecialCells(xlLastCell).Select
    Rows(ActiveCell.Row).Delete
    Range("N1").Select
    ActiveCell.FormulaR1C1 = "Balance"
    Columns("A:N").Select
    Columns("A:N").EntireColumn.AutoFit
    Range("A1").Select
    Columns("B:B").Select
    Selection.ColumnWidth = 12
    Columns("C:C").Select
    Selection.ColumnWidth = 12
    Columns("H:H").Select
    Selection.ColumnWidth = 42.57
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Sort Key1:=Range("G2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    Selection.Subtotal GroupBy:=7, Function:=xlSum, TotalList:=Array(12, 13), _
        Replace:=False, PageBreaks:=False, SummaryBelowData:=True
    With ActiveSheet.Outline
        .AutomaticStyles = False
        .SummaryRow = xlBelow
        .SummaryColumn = xlLeft
    End With
    Selection.ApplyOutlineStyles
    Columns("L:N").Select
    Selection.Style = "Comma"
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    With Selection.Interior
        .ColorIndex = 49
        .Pattern = xlSolid
    End With
    Selection.Font.ColorIndex = 2
    Selection.Font.Bold = True
    Range("A1").Select
' Balance
    ActiveCell.Offset(1, 13).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=IF(ISBLANK(C[-3]),RC[-2]-RC[-1],"""")"
    ActiveCell.Select
    Selection.AutoFill Destination:=ActiveCell.Range("A1:A244"), Type:= _
        xlFillDefault
    ActiveCell.Range("A1:A2").Select
    ActiveCell.Columns("A:A").EntireColumn.EntireColumn.AutoFit
    Selection.Style = "Comma"
End Sub

1 个答案:

答案 0 :(得分:1)

使用与此类似的代码:

Dim RowCount as Long

RowCount = Cells(Rows.Count,2).End(xlUp).Row 
'Will get the row of the last row. Replace 2 with what ever column you want it to be bassed off. Ex: B = 2

然后在你的自动填充中,你想要这样做:

Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & RowCount), Type:= _
    xlFillDefault