sumif data array

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Sumif Data Array

Sumif Data Array

resolvedResolved · Medium Priority · Version 2016

Daniel has attended:
Excel VBA Introduction course

Sumif Data Array

I've been trying to get to grips with using 2D arrays to hold data from external sheets rather than having to either copy data to my sheet or having multiple sheets open at once.

However I don't seem to be able to perform a sumif on a column in that array.

My Data is 22033 row by 37 columns, so trying to run through with a standard loop is very slow.

Is there a way to sumif the data once it is in an array and output that to a specific cell.

My existing code for the loop is this.

Set Drop = sumsht.Cells(9, 3)
lastrw = sumsht.Range("B9").End(xlDown).Row
For Each ref In sumsht.Range("B9:B" & lastrw)
Drop.Value = WorksheetFunction.VLookup(ref, linsht.Range("A:B"), 2, 0)
Drop.Offset(0, 1).Value = WorksheetFunction.VLookup(ref, linsht.Range("A:H"), 8, 0)
Drop.Offset(0, 2).Value = WorksheetFunction.VLookup(ref, linsht.Range("A:O"), 15, 0)
Drop.Offset(0, 3).Value = WorksheetFunction.SumIf(linsht.Range("A:A"), ref, linsht.Range("AC:AC"))
'Leave Discount Col blank
Drop.Offset(0, 5).Value = WorksheetFunction.SumIf(linsht.Range("A:A"), ref, linsht.Range("P:P"))
Drop.Offset(0, 6).Value = WorksheetFunction.SumIf(linsht.Range("A:A"), ref, linsht.Range("U:U"))
Drop.Offset(0, 7).Value = WorksheetFunction.Sum(sumsht.Range(Cells(ref.Row, 6), Cells(ref.Row, 9)))
Drop.Offset(0, 8).Value = WorksheetFunction.VLookup(ref, linsht.Range("A:AJ"), 36, 0)
Set Drop = Drop.Offset(1, 0)
Next ref

This is taking almost a minute to run which, while not terrible isn't great.

I have my data stored in an array like so

linarray = linsht.UsedRange.Value
ar = UBound(linarray, 1)
ac = UBound(linarray, 2)


How would I perform the same Sumifs and Lookups against the Array?

Thanks

Dan

RE: Sumif Data Array

Hi Daniel,

Thank you for the forum question.

I am sorry but the Sumif function does not work inside an array. The Vlookup does.

See link below:

https://www.ozgrid.com/forum/forum/help-forums/excel-general/67094-sumif-worksheetfunction-on-array

It looks like you are referencing all rows in your ranges Range("A:O") (I guess that linsht is an object variable to reference the sheet). This will add time to your code. Try something like:

Dim rLookuprange as Range

linsht.select
rLookuprange=range("a2").currentregion,columns("A:O")

A2 must be a cell in the range. If your data starts from row 10 change the code to:

range("a10").currentregion,columns("A:O")

and then:
Drop.Offset(0, 2).Value = WorksheetFunction.VLookup(ref,rLookuprange, 15, 0)

If you use Index Match instead of Vlookup your reference ranges will be two columns instead of 15. This should also speed up the code

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Sumif Data Array

Thanks Jens,

That is similar to what I had originally, I was playing with Arrays to try and speed things up.

On the subject of putting my worksheet on a variable, I was using this method to avoid having to have to actually select any sheets or cells as my understanding was having to select cells rather just referencing them directly was more time intensive. Is this not the case?

Thanks

Dan

RE: Sumif Data Array

Hi Daniel,

I am sorry that you had to wait for this answer.

My experience is that arrays are the fastest option for everything, but arrays have limitations (no sumif).

My suggestion was to store the ranges in range object variables, and only to store the range which are needed (only the range with the values). You are right we shouldn't select the sheet.

Change:

linsht.select
rLookuprange=range("a2").currentregion,columns("A:O")

To:

rLookuprange=linsht.range("a2").currentregion,columns("A:O")

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Shared Conditional Formatting

In a shared workbook, conditional formats applied before the workbook was shared will continue to work; however you cannot modify the existing conditional formats or apply new ones.

View all Excel hints and tips


Server loaded in 0.1 secs.