Delimma: SumIf worksheet function

Joined
Jul 2, 2012
Messages
1
Reaction score
0
Hello everyone,

I've been trying to figure out how to make this work for a while; I'm completely stumped. I have a workbook with 2 sheets that look something like this:

INV

1 B20000 10
2 B50000 32
3 B10000 15
4 B70000 50
5 B80000 7
6 B90000 0
7 B10000 63
8 B20000 80


MSTR

1 B10000
2 B20000
3 B30000

I'd like to sum the values from inv on to mstr. This could be accomplished with something like:

wsMSTR.Range("M1:M" & LR).Formula = _
"=SUMIF('INV'!$A$2:$A$20000,'MSTR'!A2,'INV'!$C$2:$C$20000)"

However, I'd infinitely prefer to use worksheetfunction.sumif in order to simplify the statement. I'll be using this code on a variety of sheets & ranges. I've attempted to do this in a number of different ways but I can't seem to get the sumif criteria right.

Here's what I'm using at the moment:

Dim wsMSTR, wsINV As Worksheet
Dim mstrLR, invLR As Long

Set wsMSTR = ActiveWorkbook.Sheets("MASTER")
Set wsINV = ActiveWorkbook.Sheets("INV")

mstrLR = wsMSTR.Range("B" & Rows.Count).End(xlUp).Row
invLR = wsINV.Range("B" & Rows.Count).End(xlUp).Row

wsMSTR.Range("M2:M" & mstrLR).Value = _
Application.WorksheetFunction.SumIf(wsINV.Range("$A$2:$A$" & invLR), _
wsMSTR.Range("$A2:$A" & mstrLR), _
wsINV.Range("$B2:$B$" & invLR))

End Sub

Or Alternatively:

wsMSTR.Range("M2:M" & mstrLR).Value = _
Application.WorksheetFunction.SumIf(wsINV.Range("$A$2:$A$" & invLR), _
wsMSTR.Range(Cells([1], 1), Cells([1], mstrLR)), _
wsINV.Range("$B2:$B$" & invLR))

This basic syntax works fine in excel but throws a type mismatch error when I try to use it in the vba worksheet function. Any suggestions or advice would be greatly appreciated.

Thanks in advance,
w_r
 
Back
Top