vba 自动填充宏到长度

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

AutoFill Macro to Length

excelvbaexcel-vba

提问by Trying_hard

I am trying to Fill a formula that I have in D1 and fill down D to the length of C. I am using the follwing macro and I am getting the following error - Compile Error: Expected end with

我正在尝试填充 D1 中的公式并将 D 填充到 C 的长度。我正在使用以下宏,但出现以下错误 - 编译错误:预期结束

Sub Macro3()

Macro3 Macro



  Range("D1").Select
  ActiveCell.FormulaR1C1 = "=RC[-2]*(-1)+RC[-1]"
  Range("D1").Select

  Dim LastRow As Long

  With Sheets("Sheet2")
  LastRow = Range("C" & Rows.Count).End(xlUp).Row
  Range("D1").AutoFill Destination:=Range("D2:D" & LastRow)
End Sub

回答by Scott Holtzman

Your problem was a simple one. I used the macro recorder to AutoFill a Formula Range and found that the Destination Range starts with the Formula Range, so

你的问题很简单。我使用宏记录器自动填充公式范围,发现目标范围以公式范围开头,所以

Range("D1").AutoFill Destination:=Range("D2:D" & LastRow)

Should be:

应该:

Range("D1").AutoFill Destination:=Range("D1:D" & LastRow)

Here is working code, both fixed and cleaned up a bit :)

这是工作代码,既修复又清理了一下:)

Sub Macro3()

With Sheets("Sheet1")

  Dim LastRow As Long
  LastRow = Range("C" & Rows.Count).End(xlUp).Row

  With Range("D1")
    .FormulaR1C1 = "=RC[-2]*(-1)+RC[-1]"
    .AutoFill Destination:=Range("D1:D" & LastRow)
  End With

End With

End Sub