5

I have the following code

 For i = 1 To DepRng.Rows.Count
    For j = 1 To DepRng.Columns.Count
         DepRng.Cells(i, j) = Application.Sum(KidsRng.Row(i)) //Does not work
    Next j
 Next i

Although I know is wrong, i have no idea how to get it to store in DepRng.Cells(i, j) the total sum of the whole KidsRng.Row[i] Any help?

4
  • Can you add in a definition of KidsRng ? Commented Nov 17, 2010 at 2:38
  • sorry i dont follow, what do you mean by adding a definition? Commented Nov 17, 2010 at 2:39
  • Seems that DepRng.Columns.Count should be 1 ... if not you are repeating the same value Commented Nov 17, 2010 at 2:41
  • tnx for your comment. Deleted my "answer" since it was just a clarification petition. Commented Nov 17, 2010 at 2:48

3 Answers 3

3

The following code works ok.

Perhaps you should compare it with yours:

Sub a()

Dim DepRng As Range
Dim kidsrng As Range
Set DepRng = Range("B1:B2")
Set kidsrng = Range("C1:F2")

 For i = 1 To DepRng.Rows.Count
      DepRng.Cells(i, 1) = Application.Sum(kidsrng.Rows(i))
 Next i

End Sub

Just fill the range C1:F2 with numbers and the totals per row will appear in B1:B2 upon execution of the macro.

Sign up to request clarification or add additional context in comments.

Comments

2

Sorted, thanks all for ur help

   DepRng.Cells(i, j) = Application.Sum(KidsRng.Rows(i)) //just needed to add the "s" in rows

1 Comment

its for another row i just had it there for now, but j will be filled with other row ranges. Sorry for the confusion though, and thanks for the help
1

There may be a better way than this, but this is my solution which depends on the internal Excel formula engine though, it might be sufficient enough for what you're doing... It determines the full address of KidsRng.Row(i), and feeds it into a =SUM() formula string and evaluated by Application.Evaluate.

For i = 1 To DepRng.Rows.Count
    For j = 1 To DepRng.Columns.Count

        DepRng.Cells(i, j).Value = Application.Evaluate("=SUM(" & KidsRng.Row(i).Address(True, True, xlA1, True) & ")")

    Next j
 Next i

updated it to work if kidsrng existed in a different sheet/book updated to use Application.Evaluate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.