Excel // Dynamic Range in Sum If Function - vba

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.

Related

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.

VBA Alternative for sumproduct function

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.

Today function equivalent in VBA in combination with countifs

I am having some problem with using a countifs formula in Excel / VBA. I have got the formula working perfect in Excel but ideally I want to use this in VBA with my form. Here is the formula in Excel which works a treat:
=COUNTIFS(Sheet1!A:A,"Place",Sheet1!K:K,"<"&TODAY())
will count the names places that are now in the past
=COUNTIFS(Sheet1!A:A,"place",Sheet1!K:K,">"&TODAY())
will count the names places that are current
I have five different Places in column A and hundreds of different dates in column K. The above formulas work well in Excel and return the correct values. I have spent hours trying to get this to work in VBA with my userform but keep getting various errors. The first part is not the problem but as soon as I get to the &today function it falls apart. From what I can see the &today function is not available in VBA and the &Date seems to be the recommendation. I have tried this but still get no where. I'm missing a trick (or several) here and I would really like to get this working in VBA rather than using the current formulas in Excel. The returned results are then displayed in textboxes on my form.
All ideas and feedback much welcome!
Second edit
================================
Thanks for the quick replies! Here is the actual code I am playing about with in VBA
'Count events by area'
Dim ListLondon As Long
ListLondon = .CountIf(Range("a1:a1998"), "London"), ("Sheet1!K1:K1998"), "<" & Date)
End With
Me.TextBox1 = ListLondon
I know the second part of the count if is all wrong regards the date - that's how I've left it for now. I am really hoping to use the current layout and a working Date / Today code at the end. Please show me what I've done wrong here!
====
oops - can see a mistake already - but the initial problem remains around the date issue. I should of used countifs as using multiple criteria.
You have to read the values of the cells to your VBA code. I recommend you to use Excel.Range object to do that. It can interpret the range like the edit line of the Excel, something like
Dim foo as Excel.Range
set foo = yourworksheet.Range("A1:B3")
Read the Date type data into VBA Date type variable by iterating through the cells.
Examine relation between the read data and the current date. Current date can be read by using the DateTime.Now function.
Increment a variable based on a decision

Evaluate a relative reference from VBA relative to a known cell

I'm using the trick described here - !A1 - to get the the range of cells up to and including the current one (typically for a Rows() function). This works great for day to day usage, and eliminates a lot of errors that I get when moving ranges around when I previously had to use an adjacent set of rows.
Unfortunately, my formulas need to be evaluatable from VBA. With __THISCELL__ as my !A1 cell, and the cell housing the formula as $Z$100 the following evaluates to an error:
Application.Evaluate(rngCell.formula)
And the following evaluates to $A$1:$Z$50
rngCell.Worksheet.Evaluate(rngCell.formula)
Obviously an approach is to replace __THISCELL__ with rngCell.Address(External:=True) prior to evaluation, but here's the kicker: I'd like to be able to execute my formula parser in a workbook which uses, say THIS_CELL, THISCELL or __THISCELL safely, and I'd also like to be able to safely execute my code in a workbook with a name like __NOT__THIS_CELL__.
All I need for this is a mechanism to evaluate relative references relative to a specific cell address - which since people do use R1C1 references in VBA a fair bit, I imagine must be around. However, I don't know it. Can anyone help?
NB: I like to avoid fiddling with ActiveCell, Selection, etc. where possible, since those smell like the excel equivalent of SendKeys - who knows what the user is doing when you access them. Even then, though, I'm not certain I'll get the right answer, because for the Worksheet.Evaluate approach, I'm not positioned in cell $A$1!
If I understand your question, I believe you're looking for the Range().Offset method.
Range().Offset(rOffset, cOffset) refers to a range that is rOffset lower and cOffset to the right of the given range (negative values for up and left are allowed). Also, .Offset can access and set all of the properties of the range, just like you would do with .Range.
The approach I've taken for the time being is implicit in the question: when a named range is detected, store the current selection and worksheet, select the one which we use as the evaluation context, and then use Evaluate. This seems to work, provided the cell being evaluated is inside the activesheet.
I don't like jumping the selection all over the place - feels dirty - but short of a more elegant solution, it does work.

How can I create sheet-specific named ranges without using Indirect?

I've come up with a useful trick, where I create a named range that refers to the current worksheet, by using the following formula:
=RIGHT(CELL("filename",INDIRECT("A1")), LEN(CELL("filename",INDIRECT("A1"))) - FIND("]",CELL("filename",INDIRECT("A1"))))&T(NOW())
Where the INDIRECTs are there ONLY to stop Excel from Converting A1 --> Sheet1!A1. This works beautifully until I need to call evaluate on it from VBA (which does happen).
Can anyone tell me how either (1) to evaluate a name with this formula in VBA or (2) to get a sheet non-specific reference into the formula. I'd rather not use VBA, since it'll get evaluated ~12000 times, and that's likely to be slow, but if need be, it's probably ok. However, please bear in mind that the sheet it is calculated from is quite unlikely to be ActiveSheet, so the context for the Range() function in VBA is a little tricky - hence why I'm asking in the first place.
One possible approach: use a simple UDF which just returns the name of the sheet it's called from.
Eg:
Function SheetName()
SheetName = Application.Caller.Parent.Name
End Function