2012-11-05 327 views
1

我有從列O - > X的公式,並需要它們將它們拖到最後一行使用。下面是當前代碼我使用:VBA拖動公式(多行)到最後一行使用

Dim wkb As Workbook 
Dim wkbFrom As Workbook 
Dim wks As Worksheet 
Dim rng As Range 
Dim path As String, FilePart As String 
Dim TheFile 
Dim loc As String 
Dim Lastrow As Long 

Set wkb = ThisWorkbook 
loc = shPivot.Range("E11").Value 
path = shPivot.Range("E12").Value 
FilePart = Trim(shPivot.Range("E13").Value) 
TheFile = Dir(path & "*" & FilePart & ".xls") 
Set wkbFrom = Workbooks.Open(loc & path & TheFile & FilePart) 
Set wks = wkbFrom.Sheets("SUPPLIER_01_00028257_KIK CUSTOM") 
Set rng = wks.Range("A2:N500") 

'Copies range from report generated to share drive and pastes into the current week tab of open order report 
rng.Copy wkb.Sheets("Current Week").Range("A4") 

With ActiveSheet 

    Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    .Range("O4:X4").AutoFill .Range("O4:X4").Resize(Lastrow) 

End With 

代碼LASTROW不拖拉公式下推

+3

是否因爲'.Resize(Lastrow)'實際上拼寫了'.Reize(Lastrow)'在代碼的底部? –

+0

是的,我發現了這個錯誤並修復了它。這不是問題。 – kmiao91

+1

嘗試錄製宏並手動執行需要完成的操作,然後修改代碼。 –

回答

10

你可以在VBA自動填充這樣

Range("O1:X1").Select 
Selection.AutoFill Destination:=Range("O1:X25"), Type:=xlFillDefault 
(使用宏錄製驗證)

現在你有這樣的代碼爲基礎與您合作可以用你喜歡的任何變量的語法如下:

Range("O1:X1").Select 
Selection.AutoFill Destination:=Range("O1:X" & Lastrow), Type:=xlFillDefault 
+0

謝謝,有一個問題,我有點困惑,我會在我的代碼中實現這個。 – kmiao91

+0

@ kmiao91您嘗試使用單元右下角的自動填充框將公式從單元格拖動到其他單元格? –

+0

正確,但公式位於O - > X列,我需要將公式拖至列A中數據的最後一行。 – kmiao91