vba VBA将公式(多行)拖到最后使用的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13240030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:22:44  来源:igfitidea点击:

VBA Dragging down formulas (in multiple rows) to last row used

vbaexcel-vbaexcel-2007excel

提问by kmiao91

I have formulas from columns O -> X and need them drag them to last row used. Below is the current code I am using:

我有来自 O -> X 列的公式,需要它们将它们拖到使用的最后一行。以下是我正在使用的当前代码:

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

The code Lastrow is not dragging the formulas down

代码 Lastrow 不会拖下公式

回答by Mike Kellogg

You can do auto-fill like this in VBA (verified using macro recording)

您可以在 VBA 中进行这样的自动填充(使用宏录制进行验证)

Range("O1:X1").Select
Selection.AutoFill Destination:=Range("O1:X25"), Type:=xlFillDefault

Now that you have this code as a base to work with you can use any variables you like in the syntax like this:

既然您已将此代码作为要使用的基础,您就可以在如下语法中使用您喜欢的任何变量:

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

回答by Mariusz Krukar

I used this:

我用过这个:

Sub sum_1to10_fill()
Sheets("13").Activate
Range("EG12").Copy
Range("EG12:FT36").PasteSpecial xlPasteFormulas

End sub

结束子

...and filled up my whole label both down and right.

...并填满了我的整个标签。