Sumif with VBA

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I'm running on empty with the following matter.


Writing this in a cell it works fine:

Code:
=SUMIF(H19:H1891,"black",G19:G1891)


Now, in my VBA code:

lastcol + 1 = column in which you can find the values to sum
lastcol + 2 = column in which you can find the criteria
criteria = "black"
row = from 19 (fix) to lr2

How can I build the calculation?


Thank's.
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Can you post a screenshot of what you data looks like? What does "lastcol" represent?
 
Upvote 0
Maybe something like this

Code:
Dim myVar as Double

myVar = Application.SumIf(Range(Cells(19, lastcol + 2), Cells(lr2, lastcol + 2)), "Black",Range(Cells(19, lastcol + 1), Cells(lr2, lastcol + 1)))

M.
 
Last edited:
Upvote 0
If you want to place the formula in a cell maybe:

Code:
lastcol = 6
lr2 = 1891
criteria = "black"
Set rng = Range(Cells(19, lastcol + 2), Cells(lr2, lastcol + 2))
Set sum_rng = Range(Cells(19, lastcol + 1), Cells(lr2, lastcol + 1))


Range("A1").Formula = "=SUMIF(" & rng.Address & ",""" & criteria & """," & sum_rng.Address & ")"
 
Upvote 0
Maybe something like this

Code:
Dim myVar as Double

myVar = Application.SumIf(Range(Cells(19, lastcol + 2), Cells(lr2, lastcol + 2)), "Black",Range(Cells(19, lastcol + 1), Cells(lr2, lastcol + 1)))

M.

Yes, perfect.

Just one clarification: why Double?
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,034
Members
448,940
Latest member
mdusw

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