sum offset range for each cell in a range by if statement

tweek

Board Regular
Joined
Jun 16, 2006
Messages
104
Hi, I´ve been trying to loop through a column range and sum a line offset range on the right of cell in the loop range. It is suppose to sum if a if statement is true but it seems like I´m getting true when actually false and therefore it sums every range.

My code is as follows:

Sub summoff()
Dim sum1 As Double
Dim sum2 As Double
Dim A As Range
Dim r As Range
Dim r1 As Range

Set r1 = Range("K475:k480")
sum2 = 0


For Each r In r1

If r.Value = 6200 Or 62001 Then


r.Offset(0, 11).Select
Set A = Selection.Resize(1, 12)

sum1 = Application.WorksheetFunction.Sum(A)

sum2 = sum2 + sum1
sum1 = 0


End If

Next

MsgBox sum2
End Sub


range K475:k480 is:
k475 blank
k476 6
k477 blank
k478 6200
k479 62001
k480 6450

offset contains various numbers.

Can anybody point out to me the right way to do this ?

Thank you.
Eric
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You are welcome.

It shouldn't be necessary to select anything

Code:
Sub summoff()
Dim sum1 As Double
Dim sum2 As Double
Dim r As Range
Dim r1 As Range
Set r1 = Range("K475:k480")
sum2 = 0
For Each r In r1
    If r.Value = 6200 Or r.Value = 62001 Then
        sum1 = WorksheetFunction.Sum(r.Offset(0, 11).Resize(1, 12))
        sum2 = sum2 + sum1
        sum1 = 0
    End If
Next r
MsgBox sum2
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,693
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top