2014-05-01 72 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