2014-05-01 28 views
-1

我希望能够通过对代码的最后一次调整获得帮助。它工作得很好,还有两个额外的手动步骤,但我希望使用宏自动完成所有操作。在最后一段,有一个我想包括在尽可能多的行,也有与他们值的行,而不只是直到行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

你尝试过这么远吗?看起来这只是从宏录像机复制和粘贴。您只需要定义一个'Range'变量来表示该列的变化长度,并使用该变量代替'ActiveCell.Range(“A1:A244”)'。 –

回答

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