“对象是必需的”

时间:2019-06-03 04:00:37

标签: excel vba

我目前正在尝试进行添加3列(Y,AA和AB)的数学运算。

但是每次运行代码时,它会显示错误消息

  

必填对象

Sub QCValue()

    Dim Number1 As Range
    Dim Number2 As Range
    Dim Number3 As Range

    Set Number1 = Sheets("RawData").Range("Y2:Y" & Range("A" & Rows.Count).End(xlUp).Row)
    Set Number2 = Sheets("RawData").Range("AA2:AA" & Range("A" & Rows.Count).End(xlUp).Row)
    Set Number3 = Sheets("RawData").Range("AB2:AB" & Range("A" & Rows.Count).End(xlUp).Row)

    Sheets("RawData").Range("AJ2:AJ" & Range("A" & Rows.Count).End(xlUp).Row).Value = Number1 + Number2 + Number3

End Sub

请帮助我进行故障排除。

2 个答案:

答案 0 :(得分:1)

您为此做了一个循环。像这样的下面应该做的工作。

Sub QCValue()

Dim i As Integer

For i = 2 To Range("A" & Rows.Count).End(xlUp).Row

    Sheets("RawData").Range("AJ" & i).Value = Sheets("RawData").Range("Y" & i).Value + Sheets("RawData").Range("AA" & i).Value + Sheets("RawData").Range("AB" & i).Value

Next

End Sub

答案 1 :(得分:0)

如果我正确理解,可以使用:

代码:

Option Explicit

Sub QCValue()

    Dim LastRow As Long

    With ThisWorkbook.Sheets("RawData")

        'Find Last rows of Columns Y
        LastRow = .Cells(.Rows.Count, "Y").End(xlUp).Row

        .Range("AJ2:AJ" & LastRow).FormulaR1C1 = "=SUM(RC[-11],RC[-9],RC[-8])"

    End With

End Sub

结果:

enter image description here