2014-08-28 82 views
-1

我想複製我的公式到行的末尾 - 但我的語法似乎是關閉的。填充公式到結束的人口範圍

Columns("D:D").Select 
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
    Range("D3").Select 
    ActiveCell.FormulaR1C1 = _ 
     "=IF(RC[1]=""low"",""1"",""0"")&IF(RC[2]=""LOW"",""1"",""0"")&IF(RC[3]=""Negative"",""1"",""0"")" 
    Range("D2").Select 
    ActiveCell.FormulaR1C1 = "Helper Column" 
    Range("D3").Select 
    **Selection.AutoFill Destination:=Selection.End(xlDown)** 
+0

和是誰坐在你的腦袋裏的魔術師,並確切地知道您的電子表格的外觀喜歡和你期待什麼作爲最終結果... – 2014-08-28 14:00:14

回答

1

您需要查找列的最後一行,然後一次輸入公式。例如(未經測試)

Sub Sample() 
    Dim ws As Worksheet 
    Dim lRow As Long 
    Dim sFormula As String 

    '~~> Change this to the relevant worksheet 
    Set ws = ThisWorkbook.Sheets("Sheet1") 

    '~~> Change this to the relevant formula 
    sFormula = "=Sum(A1:C1)" 

    With ws 
     '~~> Find Last Row of Col D 
     lRow = .Range("A" & .Rows.Count).End(xlUp).Row 

     '~~> Enter the formula 
     .Range("D3:D" & lRow).Formula = sFormula 
    End With 
End Sub 

注意:如果上校B有它定義了你的最後一行的數據,那麼改變.Range("A" & .Rows.Count).End(xlUp).Row.Range("B" & .Rows.Count).End(xlUp).Row

+0

太好了,我會測試這種方法。基本上確定列中最後一個已填充的行並將其分配給一個變量,然後利用該變量來定義範圍... – 2014-08-28 15:11:45