Somebody
Somebody

Reputation: 2779

Application.WorksheetFunction.Sum within a loop

I have this code which retrieves some data from a sql query and place it in an excel Sheet:

   I = 4
 Do While Not rs.EOF
  I = I + 1
  Sheet1.Range("A" & I).Value = rs(0)
  Sheet1.Range("B" & I).Value = rs(1)
  Sheet1.Range("C" & I).Value = rs(2)
  Sheet1.Range("D" & I).Value = rs(3)
  Sheet1.Range("E" & I).Value = rs(4)
  Sheet1.Range("F" & I).Value = rs(5)
  Sheet1.Range("G" & I).Value = rs(6)
  Sheet1.Range("H" & I).Value = rs(7)
  Sheet1.Range("I" & I).Value = rs(8)
  Sheet1.Range("J" & I).Value = rs(9)
  Sheet1.Range("K" & I).Value = rs(10)
  Sheet1.Range("L" & I).Value = rs(11)
  Sheet1.Range("M" & I).Value = rs(12)
  Sheet1.Range("N" & I).Value = rs(13)
  Sheet1.Range("O" & I).Value = rs(14)
  Sheet1.Range("P" & I).Value = rs(15)
  Sheet1.Range("Q" & I).Value = Application.WorksheetFunction.Sum(Sheet1.Range("E" & I), Sheet1.Range("P" & I))
  'Sheet1.Range("Q" & I).Value = Sheet1.Range("E" & I).Value + Sheet1.Range("F" & I).Value + Sheet1.Range("G" & I).Value + Sheet1.Range("H" & I).Value
  rs.MoveNext
 Loop

Now, I'm trying to sum a range of columns from col "E" to col "P" but it didn't work, it just sum those cells only ("E" & I and "P" & I), not the range.

Any help please?

Upvotes: 2

Views: 17276

Answers (2)

Siddharth Rout
Siddharth Rout

Reputation: 149287

Why not use a loop instead of such long code

Option Explicit

Sub Sample()
    Dim i As Long, j As Long, k As Long

    '~~> Rest of your code

    Do While Not rs.EOF
        i = i + 1
        j = 0
        With Sheets1
            k = 1
            For j = 0 To 15
                .Cells(i, k).Value = rs(j)
                k = k + 1
            Next
            .Range("Q" & i).Value = _
            Application.WorksheetFunction.Sum(.Range("E" & i & ":P" & i))
        End With
        rs.MoveNext
     Loop

     '~~> Rest of your code
End Sub

Upvotes: 3

JMax
JMax

Reputation: 26591

First, you can optimize and sanitize you code using the with statement:

Sanitize your code using with

Don't do

Sheet1.Range("A" & I).Value = rs(0)
Sheet1.Range("B" & I).Value = rs(1)
Sheet1.Range("C" & I).Value = rs(2)
...

Do

With Sheet1
    .Range("A" & I).Value = rs(0)
    .Range("B" & I).Value = rs(1)
    .Range("C" & I).Value = rs(2)
    ...
End With

Suming a range instead of unit cells

Understanding your original statement

In your original statement:

Application.WorksheetFunction.Sum(Sheet1.Range("E" & I), Sheet1.Range("P" & I))

You are calling the Worksheet Function with several arguments (as you would do with SUM(E1, P1) but you want to sum a range, i.e. SUM(E1:P1).

Solving your issue

Better try:

Application.WorksheetFunction.Sum(Sheet1.Range("E" & I & ":P" & I))

Note that you could also define your Range this way:

With Sheet1
    Application.WorksheetFunction.Sum(.Range(.Cells(I, "E"), .Cells(I, "P"))
End With

This latter can be very useful because you can use the .Cells with either a letter or a Long for the column.

Upvotes: 6

Related Questions