调整ActiveCell.FormulaR1C1

时间:2017-04-01 01:19:51

标签: excel excel-vba vba

您好我有一个问题,我有使用宏录制器制作的代码:

ActiveCell.FormulaR1C1 = "=New_Order!RC[-42]+New_Order!RC[-41]+New_Order!RC[-40]"

如何调整此代码,以便它像thi:

'ActiveCell.Formula = Sheets("New_Order").Range(n & LastRow) + Sheets("New_Order").Range(O & LastRow) + Sheets("New_Order").Range(p & LastRow)

我在N,O,P列中名为New_Order的不同工作表中有值。我想对这些值求和并自动填充工作表注册中的整个列。完整的代码如下所示:

 Sub Registrereren()

Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False

On Error Resume Next

Dim oWkSht As Worksheet
Dim LastColumn As Long
Dim c As Date
Dim myCell As Range
Dim LastRow As Long

Sheets("Registration").Activate


Set oWkSht = ThisWorkbook.Sheets("Registration")
LastColumn = oWkSht.Range("A" & Columns.Count).End(xlToRight).Column
LastRow = oWkSht.Range("C" & Rows.Count).End(xlUp).Row

c = Date

Set myCell = oWkSht.Range("1:1").Find(What:=c, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False, SearchOrder:=xlByColumns)

If Not myCell Is Nothing Then
    myCell.Offset(1, 0).Select

    ActiveCell.FormulaR1C1 = "=New_Order!RC[-42]+New_Order!RC[-41]+New_Order!RC[-40]"
    'ActiveCell.Formula = Sheets("New_Order").Range(n & LastRow) + Sheets("New_Order").Range(O & LastRow) + Sheets("New_Order").Range(p & LastRow)
    myCell.Offset(1, 0).Select
    Range(myCell.Offset(1), Cells(LastRow, myCell.Column)).Select
    Selection.FillDown

End If

Sheets("Main").Activate

Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:1)

我做了一些假设,因此进行了一些修正。我假设您在注册表的“A”列中有条目与New_Order工作表中的行数相匹配。

Option Explicit


Sub Registrereren()

  Application.Calculation = xlCalculationManual
  Application.EnableEvents = False
  Application.ScreenUpdating = False

  On Error Resume Next

  Dim oWkSht As Worksheet
  Dim LastColumn As Long
  Dim c As Date
  Dim myCell As Range
  Dim LastRow As Long

  Sheets("Registration").Activate

  Set oWkSht = ThisWorkbook.Sheets("Registration")
  LastColumn = oWkSht.Cells(1, oWkSht.Columns.Count).End(xlToLeft).Column
  LastRow = oWkSht.Cells(oWkSht.Rows.Count, "A").End(xlUp).row

  c = Date
  Set myCell = oWkSht.Cells(1, 1).Resize(, LastColumn).Find(What:=c, LookIn:=xlFormulas, _ 
               lookat:=xlWhole, MatchCase:=False, SearchOrder:=xlByColumns)

  If Not myCell Is Nothing Then
    myCell.Offset(1, 0).Formula = "=New_Order!N1+New_Order!O1+New_Order!P1"
    Range(myCell.Offset(1), Cells(LastRow, myCell.Column)).Select
    Selection.FillDown
  End If

  Sheets("Main").Activate

  Application.Calculation = xlCalculationAutomatic
  Application.EnableEvents = True
  Application.ScreenUpdating = True

End Sub

答案 1 :(得分:1)

假设您要输入的公式为:

= 'New_Order'!NR + 'New_Order'!OR + 'New_Order'!PR

其中R是包含公式的单元格的同一行,请使用此FormulaR1C1

= 'New_Order'!RC14 + 'New_Order'!RC15 + 'New_Order'!RC16

替换此行:

ActiveCell.FormulaR1C1 = "=New_Order!RC[-42]+New_Order!RC[-41]+New_Order!RC[-40]"

这一行:

ActiveCell.FormulaR1C1 = "=New_Order'!RC14 + 'New_Order'!RC15 + 'New_Order'!RC16"