VBA Alternative for sumproduct function - vba

I tried to use SUMIFS, but it failed since the formula doesn't allow me to use two different criterias from the same criteria range (which makes sense of course), so I looked around the Internet and found '=SUMPRODUCT' instead I used it but it doesn't return any value. Here my formula
=SUMPRODUCT((('BB_Juni 2016_Crew'!E:E="DB")+('BB_Juni 2016_Crew'!E:E="DZ"))*('BB_Juni 2016_Crew'!G:G))
Maybe Looping through the range with an if clause and using the sum function after "then"?
In row number 26 & 27 are two different string values ("DB,DZ") and in the same row is a value in the column (Betrag) which means amount and I need a formula to sum all values that are in the same rows as "DB" and "DZ"

I agree with Jeeped that you should "Cut your full column references down to the actual extents of your data"
There is a slight typo in your formula. Replace the * with a , and it will work just fine :)
Your original formula should be
=SUMPRODUCT((('BB_Juni 2016_Crew'!E1:E6="DB")+('BB_Juni 2016_Crew'!E1:E6="DZ")), 'BB_Juni 2016_Crew'!G1:G6)
Replace 6 with the relevant row.

This is the best I can improve your existing formula without more information.
=SUMPRODUCT(('BB_Juni 2016_Crew'!E1:INDEX('BB_Juni 2016_Crew'!E:E, MATCH("zzz", 'BB_Juni 2016_Crew'!E:E))={"DB","DZ"})*('BB_Juni 2016_Crew'!G1:INDEX('BB_Juni 2016_Crew'!G:G, MATCH("zzz", 'BB_Juni 2016_Crew'!E:E))))
It may look more complicated but it actually performs much less work.
Actually, this may be even better.
=SUM(SUMIFS('BB_Juni 2016_Crew'!G:G, 'BB_Juni 2016_Crew'!E:E, {"DB","DZ"}))
Both are standard formulas (no need for CSE).

There is a spreadsheet function AND that accepts several conditions and returns TRUE of all conditions are TRUE, FALSE otherwise.
You may try re-phrasing your formula or make your question clearer so it may be possible to give you an actual working formula.

Related

Multiple Criteria using autofilter on the same range, autofilter only takes last Criteria Used

I'm trying to filter multiple filters based on user input from a listbox on the same range.
However, for some reason the autosort method only sort the target range using the last criteria from testing.
I have searched everywhere, tested out array solutions (reading list info into an array), writing a range of values for filters on worksheets, changing variable type/operator to no avail. Nothing works.
Thanks for your time for reading this, would appreciate it if someone could help me with this.
dim lifecycle as range
dim List2String as string
Set lifeCycle = defineColRange(startWs, "Lifecycle Comments (Saks)", True, False)
For i = 0 To ListBox2_Lifecomments.ListCount - 1
'looping though the listbox2 to retrieve values
List2String = ListBox2_Lifecomments.List(i)
startWs.UsedRange.AutoFilter Field:=lifeCycle.Column, Criteria1:="<>" & List2String
Next i
startWs.UsedRange.SpecialCells(xlCellTypeVisible).Interior.Color = rgbLightPink 'testing to see if filter works
After some more digging, and rethinking, essentially I was asking the wrong questions.
The problem statement should be: "How to filter a range NOT equal to multiple criterias?"
It is documented here: Explaination of why .autosort doesn't work with not equal to with multiple criterias.
Few Solutions were discussed:
Use another a column to help you determine the result you want to achieve. In my example, it would be looping through all values from
user input, and output true/false on another column based on
comparison, then filter that helper column to work around this
problem.
Filter with an impossible values, this is situational. For example, if you want to filter numbers, set up an outofbound criteria for
non-numbers like given in the explanation link above.
Write code to hide the already filtered criteria, and keep filtering what is left with rest of criteria, 2 at a time.
Use advanced filters
Hope this helps others who might get into same problems in the future.

SUMIFS returns 0 using dynamic criteria, with criteria range and sum range on another sheet

Anyone,
I've chatted with and called excel customer service with no luck. I used the formula builder (please see attached screenshot) to make sure each element of the formula is correct and returns the value for the criteria I'm trying to reference.
Everything is accurate, but it returns a value of 0. When I do the same thing in the actual sheet the data is stored in (and click a criteria cell within the criteria range) it returns the accurate value?! I'm not sure why it won't work on the other sheet. The values I am using to select are dynamic and change with a drop down. I have another, advanced, workbook (I did not create) that does the same thing and completes an even more complicated formula, but actually works so I'm not sure why this is returning a 0 value.
Photos and code/syntax: Dynamic Selection, Example 2 of it working, Example 1 of it working, Formula Builder, CountIFs, Advanced Spreadsheet working, VLOOKUP
=SUMIFS('GFEBS Pull'!Q:Q,'GFEBS Pull'!G:G,FMCOP!$C$20,'GFEBS Pull'!H:H,FMCOP!B23)
or:
=SUMIFS('GFEBS Pull'!Q:Q,'GFEBS Pull'!G:G,'FMCOP'!$C$20,'GFEBS Pull'!H:H,'FMCOP'!B23)
When I type ' around FMCOP sheet name, they disappear? I've also tried to lock the columns on the 'GFEBS Pull' sheet with no luck. Cell B23 is not locked because I'm going to copy the formula down to reference other cells. Any help is appreciated!
In this screenshot you can clearly see that both FMCOP!C20 ansd FMCOP!B23 have prefacing spaces; e.g. " HHC".
Since " HHC" will never match "HHC", fix the data returned from 'the lower table in the same screenshot'.
A Text-to-Columns, Fixed Width, Finish should do this. You could adjust the original formula like,
=SUMIFS('GFEBS Pull'!Q:Q, 'GFEBS Pull'!G:G, TRIM(FMCOP!$C$20), 'GFEBS Pull'!H:H, TRIM(FMCOP!B23))
I would caution against the latter 'bandaid' fix. Fix the original data; do not apply bandaids on-the-fly.

Inserting vlookup function via vba adds brackets on column

I've got a problem with brackets in complex vlookup "insert function".
It's needed for a plan for each day of week. Because of slow performance of workbook I decided to turn on/off each day of plan, entire plan works but the column with expeditions. Code for part of "U" column is:
Range(Cells(firstm, "U"), Cells(lastm, "U")) = "=IF(RC2=0,IF(RC[-8]>0,
IF(ISERROR(VLOOKUP(G:G,Expedition!F:H,3,0)),R1C22,VLOOKUP(G:G,Expedition!F:H,3,0)),R1C22)
,IF(ISERROR(VLOOKUP(G:G,Expedition!F:H,3,0)),R1C22,VLOOKUP(G:G,Expedition!F:H,3,0)))"
It's quite messy, but it works :) at least in sheet.
The problem is that part of inserted formula in cell is
VLOOKUP(G:(G);Expedition!F:(H);3;0)
instead of
VLOOKUP(G:G;Expedition!F:H;3;0)
if only VLOOKUP is used, it works fine, I have no idea why in this example are those brackets.
Any ideas?
//edited for better understanding
Try to use either R1C1 or A1 reference style and specify the formula type, for example:
Range(Cells(firstm, "U"), Cells(lastm, "U")).FormulaR1C1 = "=IF(RC2=0,IF(RC[-8]>0, IF(ISERROR(VLOOKUP(C[7],Expedition!C[6]:C[8],3,0)),R1C22,VLOOKUP(C[7],Expedition!C[6]:C[8],3,0)),R1C22) ,IF(ISERROR(VLOOKUP(C[7],Expedition!C[6]:C[8],3,0)),R1C22,VLOOKUP(C[7],Expedition!C[6]:C[8],3,0)))"
edit: of course the relative references G:G etc have to be changed to relative references C[7]. C[7] becomes G:G while C7 becomes $G:$G.
Well, it perfectly works.. when I'm using that in half of the code, why haven't I used that for columns?.. :) Thank you!

Excel // Dynamic Range in Sum If Function

I’m trying to have a quite complicated Sum if function more reliable. The situation is that I’m using that formula (see below) to sum values between two dates from a separate sheet in the workbook.
=SUMIFS(wochen!$F11:$BM11;wochen!$F$8:$BM$8;">="&DATE(YEAR(T$8);1;1);wochen!$F$8:$BM$8;"<="&DATE(YEAR(T$8);MONTH(T$8);DAY(T$8)))
The handicap is that I was hoping to implement some Kind of Lookup function or match function to always get the values from the correct row.
So far the sum_range is static and I can’t make sure its picking the correct line.
I already played around with this kind Address / Match Function
=ADDRESS(MATCH($B13;'F_P&L'!$B$1:$B$267;0);MATCH(T$6;'F_P&L'!$F$6:$CP$6;0);;;"wochen!")
into that Sum_Range of the Sum If Formular but that doesn’t work either as it comes out as Text
I’m happy for any Idea which van be implemented via excel Formulas or VBA
I hope I explained everything clear, if not I'm happy to provide more information's
Many Thanks in Advance
Dennis
Here is a simple example which you can extend to your case. It uses the =INDIRECT() formula.
If you know through some way what the correct rows are, say you know your range of interest is in line 11, ...
Put the following in a cell
A1: = 11 ' You can use VLOOKUP etc. to generate the row number
B1: = "F" & A1 & ":BM"&A1 ' That makes B1 read: F11:BM11
C1: = SUM(INDIRECT(B1)) ' This will make it the equivalent of writing =SUM(F11:BM11)
Writing =function(INDIRECT(*anything*)) where *anything* is a string describing the address of a range is the equivalent of writing =function(** selecting the range **)
Note on Performance: Do note, however, that INDIRECT is a volatile functions so any changes in the workbook with calculations set to automatic will cause it to recalculate. So there might be performance implications in certain cases.

Column references in formulas

I am a little stuck at the moment. I am working on an array of data and need to find a way to input column numbers into formulas.
-I have used the match function to find the corresponding column number for a value.
ex. "XYZ" matched with Column 3, which is equivalent to C1:Cxxxxxx
-now for inputing the C1:Cxxxxxx into a formula to get data for that particular column, I would like to be able to directly reference the Column 3 part, because I plan on using this workbook in the future and the column needed to run the calculation may or may not be column 3 the next time I use it.
- is there any way to tell excel to use a formula to tell excel which column to use for an equation?
so a little more detail, I have the equation
=AND(Sheet3!$C$1:$C$250000=$A$4,Sheet3!$B$1:$B$250000=$B$4)
instead of specifying to use column C, is there a way to use a formula to tell it to use C?
EDIT: more additional info;
"i am basically running the equivalent of a SQL where statement where foo and bar are true, I want excel to spit out a concatenated list of all baz values where foo and bar are true. ideally i would like it to ONLY return baz values that are true, then I will concat them together separately. the way I got it now, the expression will test every row separately to see if true; if there is 18K rows, there will be 18K separate tests.. it works, but it's not too clean. the goal is to have as much automated as possible. *i do not want to have to go in and change the column references every time I add a new data arra*y"
Thanks
You can use INDEX, e.g. if you have 26 possible columns from A to Z then this formula will give you your column C range (which you can use in another formula)
=INDEX(Sheet3!$A$1:$Z$250000,0,3)
The 0 indicates that you want the whole column, the 3 indicates which column. If you want the 3 can be generated by another formula like a MATCH function
Note: be careful with AND in
=AND(Sheet3!$C$1:$C$250000=$A$4,Sheet3!$B$1:$B$250000=$B$4)
AND only returns a single result not an array, if you want an array you might need to use * like this
=(Sheet3!$C$1:$C$250000=$A$4)*(Sheet3!$B$1:$B$250000=$B$4)
You could use ADDRESS to generate the text, you then need to use INDIRECT as you are passing a string rather than a range to the fomula
=AND(INDIRECT(ADDRESS(1,3,,,"Sheet3") & ":" & ADDRESS(250000,3))=$A$4
,INDIRECT(ADDRESS(1,2,,,"Sheet3") & ":" & ADDRESS(250000,2))=$B$4)
Obviously replace the 3s and 2s in the ADDRESS formulae with your MATCH function you used to get the column number. The above assumes the column for $B$1:$B$25000 is also found using `MATCH', otherwise it is just:
=AND(INDIRECT(ADDRESS(1,3,,,"Sheet3") & ":" & ADDRESS(250000,3))=$A$4
,Sheet3!$B$1:$B$25000=$B$4)
Note a couple of things:
You only need to use "Sheet3" on the first part of the INDRECT
Conditions 3 and 4 in the ADDRESS formula are left as default, this
means they return absolute ($C$1) reference and are A1 style as
opposed to R1C1
EDIT
Given the additional info maybe using an advanced filter would get you near to what you want. Good tutorial here. Set it up according to the tutorial to familiarise yourself with it and then you can use some basic code to set it up automatically when you drop in a new dataset:
Paste in the dataset and then use VBA to get the range the dataset uses then apply the filter with something like:
Range("A6:F480").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Sheets("Sheet1").Range("A1:B3"), Unique:=False
You can also copy the results into a new table, though this has to be in the same sheet as the original data. My suggestion would be paste you data into hidden columns to the left and put space for your criteria in rows 1:5 of the visible columns and then have a button that gets the used range for your data, applies the filter and copies the data below the criteria:
Range("A6:F480").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Sheets _
Range("H1:M3"), CopyToRange:=Range("H6"), Unique:=False
Button would need to clear the destination cells first etc, make sure you have enough hidden columns etc but it's all possible. Hope this helps.