vba 宏中的自动填充公式从 A 列到最后一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15874830/
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-11 20:28:29  来源:igfitidea点击:

Autofill formulas in Macro from column A to last column

vbadynamicautofill

提问by Daniel

Im trying to Make a macro which will take the fomulas in cells B358 to B362 and autofill these rows to the last column used in row 2.

我正在尝试制作一个宏,它将采用单元格 B358 到 B362 中的公式并将这些行自动填充到第 2 行中使用的最后一列。

Im pretty new at VBA and completely selftaught by searching forums like this but unfortunately im not finding my solution.

我在 VBA 方面很新,通过搜索这样的论坛完全自学,但不幸的是我没有找到我的解决方案。

Please help me

请帮我

Edit:

编辑:

If you need to see some code, i can give you a very basic example

如果你需要看一些代码,我可以给你一个非常基本的例子

Sub Macro1()

    Range("B358:B362").Select
    Selection.AutoFill Destination:=Range("B358:AHQ362"), Type:=xlFillDefault
    Range("B358:AHQ362").Select
End Sub

In this example i would need the AutoFill destination to be (B358:"insert last column used in row 2 here"362)

在这个例子中,我需要自动填充目标是 (B358:"insert last column used in row 2 here"362)

采纳答案by Kazimierz Jawor

That is proposed dynamic solution in your situation:

这是在您的情况下建议的动态解决方案:

Sub Macro1()


Dim lastCol As Long
lastCol = Cells(2, Columns.Count).End(xlToLeft).Column

 Range("B358:B362").AutoFill Destination:=Range(Range("B358"), Cells(362, lastCol)), Type:=xlFillDefault

End Sub