VBA中的求和范围循环

时间:2016-01-08 13:30:20

标签: vba for-loop sum

我想将第I列的行与T相加,并在第V列显示结果。

目前,我的代码是:

Sub Sum_column_V()

Dim lastRow As Long, i As Integer, totalItoT As Double, sht As Worksheet

Set sht = ThisWorkbook.Worksheets("Summary")

lastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

For i = 1 To lastRow
    totalItoT = WorksheetFunction.Sum(Range("I" & i & "T" & i))
Next
    sht.Range("V" & i) = totalItoT

End Sub

我收到错误消息:"运行时错误' 1004':方法'范围'对象'全球失败"

我做错了什么?

1 个答案:

答案 0 :(得分:0)

使用Nathan_Sav校正的初始宏使代码工作。但是,使用不同的方法来优化运行时间。这是:

  var listOfCustomData = dbContext.someEntity.Select(x => new
                        {
                            MyId = x.MyId,
                            YourId = x.MyId 
                        }).ToList();

                        var DataWithinNetworklist = dbContext.Users.Where(u =>
                            listOfCustomData.Any
                            (   x => x.MyId == u.MyId &&
                                x.YourId == u.YourId)
                            )
                            .Select(u => u.UserId)
                            .ToList();