VBA with sumif function while using condition

gurtejhira

New Member
Joined
Jul 28, 2015
Messages
26
I have two rows A and B . I like to write a VBA with sumif statement . sum the data in B if the first letter in column A is 4 or 6 or 7.

Below mentioned is the code i tried but somehow its giving me only zero value. Please help me to identify what wrong i m doing.



Sub YardiDASR()
Dim a As Long, b As Long, c As Long
Dim lastrow As Long
Dim mycell As Range
a = 0
b = 0
c = 0
d = 0

lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

For i = 3 To lastrow

If Left(Cells(i, 1), 1) = "4" Then
a = Cells(i, 7) + a
ElseIf Left(Cells(i, 1), 1) = "6" Then
b = Cells(i, 7) + b
ElseIf Left(Cells(i, 1), 1) = "7" Then
c = Cells(i, 7) + c
Else
d = Cells(i, 7) + d
End If
Next i

Range("J2") = a
Range("K2") = b
Range("L2") = c
Range("M2") = d

End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Re: How to write vba with sumif function while using condition

Try this:

Code:
Sub YardiDASR()
Dim lastrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("J2").Formula = "=SUMPRODUCT(--(LEFT(A3:A" & lastrow & ")=""4""))"
Range("K2").Formula = "=SUMPRODUCT(--(LEFT(A3:A" & lastrow & ")=""6""))"
Range("L2").Formula = "=SUMPRODUCT(--(LEFT(A3:A" & lastrow & ")=""7""))"
Range("M2").Formula = "=COUNTA(A3:A" & lastrow & ")-SUM(J2:L2)"
Range("J2:M2").Copy
Range("J2:M2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
 
Upvote 0
Re: How to write vba with sumif function while using condition

Scott, I m getting count of the row but i m looking for sum of the fields.
 
Upvote 0
Re: How to write vba with sumif function while using condition

Code:
Sub YardiDASR()
Dim lastrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("J2").Formula = "=SUMPRODUCT(--(LEFT(A3:A" & lastrow & ")=""4""),a3:a" & lastrow & ")"
Range("K2").Formula = "=SUMPRODUCT(--(LEFT(A3:A" & lastrow & ")=""6""),a3:a" & lastrow & ")"
Range("L2").Formula = "=SUMPRODUCT(--(LEFT(A3:A" & lastrow & ")=""7""),a3:a" & lastrow & ")"
Range("M2").Formula = "=SUM(A3:A" & lastrow & ")-SUM(J2:L2)"
Range("J2:M2").Copy
Range("J2:M2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
 
Upvote 0
Re: How to write vba with sumif function while using condition

Can you post some sample data?

This is what I got after running my code


Excel 2010
ABCDEFGHIJKLM
1
24400070005000
3400
44000
55000
67000
Sheet1
 
Last edited:
Upvote 0
Re: How to write vba with sumif function while using condition

A b
4010-0000 12500
4040-0000 500
6310-0000 21000
6415-0000 900

a = 13000
b =21900
 
Upvote 0
Re: How to write vba with sumif function while using condition

Code:
Sub YardiDASR()
Dim lastrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("J2").Formula = "=SUMPRODUCT(--(LEFT(A3:A" & lastrow & ")=""4""),B3:B" & lastrow & ")"
Range("K2").Formula = "=SUMPRODUCT(--(LEFT(A3:A" & lastrow & ")=""6""),B3:B" & lastrow & ")"
Range("L2").Formula = "=SUMPRODUCT(--(LEFT(A3:A" & lastrow & ")=""7""),B3:B" & lastrow & ")"
Range("M2").Formula = "=SUM(B3:B" & lastrow & ")-SUM(J2:L2)"
Range("J2:M2").Copy
Range("J2:M2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,832
Messages
6,181,235
Members
453,026
Latest member
cknader

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