2016-10-14 146 views
0

我希望我的宏在表单中填充我的公式。每次运行宏时,它都停在13662行,因为那是我录制它时的最后一行,但最后一行不断变化。有没有办法做到这一点? 我需要这样做的列是N,I,J,K和L.任何帮助表示赞赏。见下面的代码。宏自动填充到最后一个相邻单元格

Sub Weekly_Expiring_Rebate_Report() 
' 
' Weekly_Expiring_Rebate_Report Macro 
' 

' 
    Columns("A:A").Select 
    Range(Selection, Selection.End(xlToRight)).Select 
    With Selection 
     .VerticalAlignment = xlBottom 
     .WrapText = True 
     .Orientation = 0 
     .AddIndent = False 
     .IndentLevel = 0 
     .ShrinkToFit = False 
     .ReadingOrder = xlContext 
     .MergeCells = False 
    End With 
    With Selection 
     .VerticalAlignment = xlBottom 
     .WrapText = False 
     .Orientation = 0 
     .AddIndent = False 
     .IndentLevel = 0 
     .ShrinkToFit = False 
     .ReadingOrder = xlContext 
     .MergeCells = False 
    End With 
    Columns("A:A").Select 
    Range(Selection, Selection.End(xlToRight)).Select 
    Columns("A:Z").EntireColumn.AutoFit 
    Columns("N:N").Select 
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
    Selection.NumberFormat = "General" 
    Range("N1").Select 
    ActiveCell.FormulaR1C1 = "Expiring Rebate Status" 
    Range("N2").Select 
    Columns("N:N").EntireColumn.AutoFit 
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],'Status IND'!C[-13]:C[-11],3,FALSE)" 
    Range("N2").Select 
    Selection.AutoFill Destination:=Range("N2:N13662") 
    Range("N2:N13662").Select 
    Columns("I:I").Select 
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
    Range("I2").Select 
    ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],4)" 
    Range("I2").Select 
    Selection.AutoFill Destination:=Range("I2:I13662") 
    Range("I2:I13662").Select 
    Range("J2").Select 
    ActiveCell.FormulaR1C1 = "=LEFT(RC[-2],6)" 
    Range("J2").Select 
    Selection.AutoFill Destination:=Range("J2:J13662") 
    Range("J2:J13662").Select 
    Range("K2").Select 
    ActiveCell.FormulaR1C1 = "=RIGHT(RC[-1],2)" 
    Range("K2").Select 
    Selection.AutoFill Destination:=Range("K2:K13662") 
    Range("K2:K13662").Select 
    Range("L2").Select 
    ActiveCell.FormulaR1C1 = "=RIGHT(RC[-4],2)" 
    Range("L2").Select 
    Selection.AutoFill Destination:=Range("L2:L13662") 
    Range("L2:L13662").Select 
    Range("I1").Select 
    ActiveCell.FormulaR1C1 = "EXP_YEAR" 
    Range("K1").Select 
    ActiveCell.FormulaR1C1 = "EXP_MONTH" 
    Range("L1").Select 
    ActiveCell.FormulaR1C1 = "EXP_DAY" 
    Columns("I:L").Select 
    Range("L1").Activate 
    Selection.Copy 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 
    Columns("J:J").Select 
    Application.CutCopyMode = False 
    Selection.Delete Shift:=xlToLeft 
    Range("L1").Select 
    ActiveCell.FormulaR1C1 = "EXP_Month_Name" 
    Columns("I:L").Select 
    Range("L1").Activate 
    Columns("I:L").EntireColumn.AutoFit 
    Range("L2").Select 
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],Month!C[-11]:C[-10],2,FALSE)" 
    Range("L2").Select 
    Selection.AutoFill Destination:=Range("L2:L13662") 
    Range("L2:L13662").Select 
    ActiveWindow.SmallScroll Down:=-6 
    Columns("M:M").Select 
End Sub 
+0

请重新格式化您的代码,使其可读。 – Trashman

+1

对不起。我对此很陌生,但我相信我已经修好了。 – PNigb

+0

为什么你不能拖动单元格的右下角有公式和填充。另外,你可以看到N2:N13662,当然它会在那里停下来。更改该号码(N13662)。 –

回答

0

看起来你的问题是从第5行开始。

基于this answer你可以取代:

Selection.AutoFill Destination:=Range("L2:L13662")

Selection.AutoFill Destination:=Range("L2:L" & ActiveSheet.UsedRange.Rows.Count)

+0

感谢您的回复。我试了这个,得到了运行时错误'1004':对象'_Global'的方法'范围'失败 – PNigb

+0

@PNigb - 啊,我有一个错字,删除了范围第二部分的“L”。它为我运行没有错误,但没有你的输入数据我不是100%确定。我更新了上面的答案 - 它应该是“L2:L” - 你只更新行,而不是列。 – 0w3n

+0

这就像一个魅力!非常感谢您的帮助。 – PNigb

0

这里是我认为将满足您的需要的VBA代码的例子。

'Counts the number of rows in column "C" and then places 
'concatenation formula in each adjacent cell (in column "D"). 
     Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row).Formula = "=CONCATENATE(C2,"", "",B2)" 
相关问题