How to sum up values between 2 dates in Excel 2007 - excel

Lets say I would like to sum up values from January to March. Below is an example
╔═══════════╦════════════╗
║ Column A ║ Column B ║
╠═══════════╬════════════╣
║ 1/30/2011 ║ 1 ║
║ 1/25/2011 ║ 1 ║
║ 3/30/2011 ║ 1 ║
║ 3/25/2011 ║ 1 ║
║ 5/13/2011 ║ 1 ║
╚═══════════╩════════════╝
I did some research and found I can use the SUMIFS function
=SUMIFS(B1:B5,A1:A5,">="&DATE(YEAR(2011),MONTH(1),DAY(1)),A1:A5,"<="&DATE(YEAR(2011),MONTH(4),DAY(1)))
But for some reason instead of returning 4 it returns 0. I would really appreciate if someone could figure out why.
Thank you

I don't think Year/Month/Day do what you're expecting, see:
http://www.techonthenet.com/excel/formulas/year.php
They return the year value, month value, and day value of their argument. Try entering
=YEAR(2011)
and compare it to
=YEAR("1/30/2011")
That said, you can get what you want by just putting the dates in the quotes
=SUMIFS(B1:B5,A1:A5,">=2011-01-01",A1:A5,"<=2011-04-01")
produces 4 in my Excel.

Related

How to a create field which lists the headers of previous fields containing "yes"?

I have a table in excel of the following format:
╔════════╦════════╦════════╦════════╦═════════╗
║ Field1 ║ Field2 ║ Field3 ║ ... ║ Field10 ║
╠════════╬════════╬════════╬════════╬═════════╣
║ no ║ no ║ no ║ ... ║ no ║
║ no ║ yes ║ no ║ ... ║ no ║
║ yes ║ yes ║ yes ║ ... ║ yes ║
║ yes ║ yes ║ no ║ ... ║ yes ║
║ . ║ . ║ . ║ ... ║ . ║
║ . ║ . ║ . ║ ... ║ . ║
║ . ║ . ║ . ║ ... ║ . ║
╚════════╩════════╩════════╩════════╩═════════╝
Where as you can see, each field can have any combination of yes's and no's
I am trying to create a single field which is based on the data format previously shown. This field will contain the name(s) of fields which contained a "yes". If a "yes" exists in more than one Field, then it should list those fields separated by a comma.
Here is an example of what that desired field might look like:
╔════════╦════════╦════════╦════════╦═════════╦══════════════════════════╗
║ Field1 ║ Field2 ║ Field3 ║ ... ║ Field10 ║ NewField ║
╠════════╬════════╬════════╬════════╬═════════╬══════════════════════════╣
║ no ║ no ║ no ║ ... ║ no ║ ║
║ no ║ yes ║ no ║ ... ║ no ║ Field2 ║
║ yes ║ yes ║ yes ║ ... ║ yes ║ Field1, ..., Field10 ║
║ yes ║ yes ║ no ║ ... ║ yes ║ Field1, Field2, Field10 ║
║ . ║ . ║ . ║ ... ║ . ║ ... ║
║ . ║ . ║ . ║ ... ║ . ║ ... ║
║ . ║ . ║ . ║ ... ║ . ║ ... ║
╚════════╩════════╩════════╩════════╩═════════╩══════════════════════════╝
I am trying to achieve this with an excel formula, but so far the only solution apparent to me involves including every possible permutation in the excel formula. Of course, this is inefficient and time-consuming to create and to make changes to. Is there any way to achieve this result efficiently?
If you do not have Office 365 Excel then here is a Custom UDF to do what you want:
Function JoinField(ttl As Range, srchrng As Range, crit As Variant, Optional sep As String = ",") As String
Dim ttlArr() As Variant
Dim srchrngArr() As Variant
Dim i&,j&
ttlArr = ttl.Value
srchrngArr = srchrng.Value
If UBound(ttlArr, 1) <> UBound(srchrngArr, 1) Or UBound(ttlArr, 2) <> UBound(srchrngArr, 2) Then Exit Function
For i = LBound(ttlArr, 1) To UBound(ttlArr, 1)
For j = LBound(ttlArr, 2) To UBound(ttlArr, 2)
If srchrngArr(i, j) = crit Then
JoinField = JoinField & ttlArr(i, j) & sep
End If
Next j
Next i
JoinField = Left(JoinField, Len(JoinField) - Len(sep))
End Function
Put this in a module attached to the workbook. DO NOT put it in the worksheet code or ThisWorkbook code.
It is then called like a normal function:
=JoinField($A$1:$J$1,$A2:$J2,"Yes",",")
Where the first criteria is the names to concatenate. The second is the Range that has the criteria. The third is the Criteria to find. The forth is optional separation character. Default is ,.
If you have the latest Office 365 Excel you can use this formula as an array.
=TEXTJOIN(",",TRUE,IF($A2:$J2 = "Yes", $A$1:$J$1,""))
Being an array it needs to be confirmed with Ctrl-Shift-Enter when exiting edit mode instead of Enter. If done correctly Excel will put {} around the formula.
As a demonstration of how tedious and verbose the FORMULA would get using multiple IF formulas, you could wind up with a formula looking like the following:
=IF(LEFT(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""),1)=",",SUBSTITUTE(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""),", ","",1),IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""))
The above formula assumed row 1 as your field headers, and your first field starting column A. The above formula would be placed in K2 and copied down.
Now if for some reason you are not allowed to use VBA or save your your worksheet in and .XLSM format, then you would need something like that hideous formula above.
With out the scroll bar, the formula looks more like:
=IF(LEFT(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&
IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&
IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&
IF(J2="yes",", "&J$1,""),1)=",",
SUBSTITUTE(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&
IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&
IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&
IF(J2="yes",", "&J$1,""),", ","",1),
IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&
IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&
IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&
IF(J2="yes",", "&J$1,""))
Proof of ugliness at work
A major down side of this is that if you needed to add another field the editing of the formula would be a royal pain in the butt!
If theres only 10 Columns you could go with sth. like this
=IF(A2=1;INDIRECT("A1";TRUE);"") & ", " & IF(B2=1;INDIRECT("B1";TRUE);"") & ", " & ....
In this Example a just added 0, 1 as values not the yes/no.

How to filter and list data from another sheet?

I need some help for my Excel formula. I am trying to select specific rows based on the values in those rows.
Let's say these are my values:
╔══════════╦═════════╦══════╗
║ Data ║ Month ║ Name ║
╠══════════╬═════════╬══════╣
║ Value 1 ║ january ║ mark ║
║ Value 2 ║ january ║ mark ║
║ Value 3 ║ january ║ rick ║
║ Value 4 ║ january ║ rick ║
║ Value 5 ║ march ║ mark ║
║ Value 6 ║ march ║ mark ║
║ Value 7 ║ march ║ rick ║
║ Value 8 ║ march ║ rick ║
║ Value 9 ║ august ║ mark ║
║ Value 10 ║ august ║ rick ║
╚══════════╩═════════╩══════╝
The value in A1 = january
The value in A2 = mark
I want to list all the rows where the month is A1 and the name is A2.
This should be my result:
║ Value 1 ║ january ║ mark ║
║ Value 2 ║ january ║ mark ║
I have tried using INDEX combined with IF, but with no succes. I am looking for either a VBA solution or a formula, whatever is best.
I am assuming that:
'Data' is in Sheet1!A1, 'Month' is in Sheet1!B1 and 'Name' is in Sheet1!C1.
On Sheet2!A1 is the value of 'Month' which you want to filter out and on Sheet2!A1 is the value of 'Name' you want to filter out.
Make a new column in Sheet1!D1 (beside 'Name') and give it a header, eg. 'Tag'. In the first row, D1 (under 'Tag') write the formula
=IF(AND(B2=Sheet2!$A$1,C2=Sheet2!$A$2),MAX($D$1:D1)+1,"")
Pull (copy) the above formula down till the last row of your table.
On Sheet2, replicate your table headers, this time in B1, C1 and D1 (as A1 & A2 already have your filter values).
// now everything is for Sheet2
In B2 write the following formula:
=IFERROR(INDEX(Sheet1!A:A,MATCH(ROWS($B$2:B2),Sheet1!$D:$D,0)),"")
Paste this formula in the new filter table rows.
Change values in Sheet2!A1 (Month) and Sheet2!A2 (Name) to test if the correct rows are being filtered.

Wildcard condition with * for SUMIFS formula on a column with number values

I have a huge excel sheet that looks like this:
╔══════╦══════╦═════╗
║ A ║ B ║ C ║
╠══════╬══════╬═════╣
║ Jack ║ 2001 ║ 1,5 ║
║ Jack ║ 2002 ║ 2,0 ║
║ Jack ║ 2003 ║ 1,0 ║
║ Jack ║ 3001 ║ 3,5 ║
║ Jack ║ 3002 ║ 4,0 ║
║ Jack ║ 3003 ║ 1,0 ║
║ Jill ║ 2001 ║ 3,0 ║
║ Jill ║ 2002 ║ 5,0 ║
║ Jill ║ 2003 ║ 2,0 ║
║ Jill ║ 3001 ║ 0,5 ║
║ Jill ║ 3002 ║ 6,0 ║
║ Jill ║ 3003 ║ 2,5 ║
╚══════╩══════╩═════╝
Column B contains many different numbers, but they all begin with digits 2, 3 or 8. The numbers in column B are always be 4 digits long; I'm only interested in checking the first digit.
I need to add together the values of column C, where the first digit of the corresponding cell in column B is either 2*, 3* or 8*. What I need is to create a formula that does this (Ruby-esque pseudocode):
sum = 0
spreadsheet_rows.each do |row|
if row.a == "Jack" and row.b == "2*" # Note the second wildcard condition.
sum += row.c
end
end
puts sum # Should print 4,5 in this example.
I'm trying to use the following formula in Excel to accomplish this:
=SUMIFS($C:$C; $A:$A; "Jack"; $B:$B; "=2*")
I know that Excel does not support wildcard conditions for numbers, however, I have formatted column B as type "Text" in Excel, so I thought it would be treated as such, but it appears that it is still treated as an int.
Is there a different way of applying a wildcard condition in =SUMIFS for number values in Excel? Perhaps there's a way to somehow "cast" the integers to strings in the formula? I haven't found a way to do it (yet).
I'm using Excel for Mac 2011.
I'd go for the less readable, but more powerful SUMPRODUCT:
=SUMPRODUCT(($A:$A="Jack") * (LEFT($B:$B;1)="2") * ($C:$C))
which will generate boolean arrays for each of the conditions (first and second brace part) which it will multiply with the third one (your numbers).
EDIT:
As noted in comments, #VALUE errors can appear if any value in column C cannot be converted to a number. To avoid that, you could use the syntax suggested by barry houdini
=SUMPRODUCT(($A:$A="Jack") * (LEFT($B:$B;1)="2"); $C:$C)
and let SUMPRODUCT skip over non-numbers.
This works for me:
=SUM((A1:A12=F2)*(LEFT(B1:B12)=""&F3)*C1:C12)
entered as an array formula with CtrlShiftEnter
You ask how to cast numbers to strings; concatenating an empty string and a number ""&F3 is one way to do that.
{=SUM((A1:A12=F2)*(LEFT(B1:B12)=""&F3)*C1:C12)}
No need for an arrays as shown above just type following formula which gives equal results to above Arrays formula
=SUMPRODUCT((A1:A12=F2)*(LEFT(B1:B12)=F3&"")*C1:C12)
Remember difference [Arrays=""&F3] vs [SUMPRODUCT= F3&""]
I shall be bring to your kind I am just very very very happy to see your work and the way you authoritatively asked question as as
"I know that Excel does not support wildcard conditions for numbers, however, I have
formatted column B as type "Text" in Excel, so I thought it would be treated as
such, but it appears that it is still treated as an int."
SUMPRODUCT only not work when we have to get output in TEXT
Hoping your verification about all above and kindly highlight at prominent place

VBA to merge cells in excel

I have a excel sheet like below. Consider the sno column as Excel numbering.
So all the columns except DateTime are merged. I want to Merge the DateTime cells and want to delete the empty records. I tried a few formulas with no success. Is this possible
I tried below formula but its giving weird results becoz of datetime values i think.
=INDEX($A$1:$A$<ENDROW>,(B1-1)*2+1,1)&" "&INDEX($A$1:$A$<ENDROW>,(B1-1)*2+2,1)
I have this:
╔═════╦════════╦══════════╦═════════╦═════════════╗
║ sno ║ Action ║ DateTime ║ User ID ║ Name ║
╠═════╬════════╬══════════╬═════════╬═════════════╣
║ 1 ║ INSERT ║ 8-Nov-13 ║ childsk ║ Keri Childs ║
║ 2 ║ ║ 16:06:43 ║ ║ ║
║ 3 ║ INSERT ║ 8-Nov-13 ║ childsk ║ Keri Childs ║
║ 4 ║ ║ 16:04:27 ║ ║ ║
╚═════╩════════╩══════════╩═════════╩═════════════╝
Expected Output:
╔═════╦════════╦═══════════════════╦═════════╦═════════════╗
║ sno ║ Action ║ DateTime ║ User ID ║ Name ║
╠═════╬════════╬═══════════════════╬═════════╬═════════════╣
║ 1 ║ INSERT ║ 8-Nov-13 16:06:43 ║ childsk ║ Keri Childs ║
║ 2 ║ INSERT ║ 8-Nov-13 16:04:27 ║ childsk ║ Keri Childs ║
╚═════╩════════╩═══════════════════╩═════════╩═════════════╝
You have two tasks to perform.
To merge date and time into a single cell. This is simply done by (Cell C1 contains the formula (as text) that is used in B1.)
and formatting B1 as Custom -> d-mmm-yy hh:mm:ss.
Then you can copy the results (as values) and format to a destination column.
Remove extra rows. You can use the code below (make sure you change the number of repetitions from 10 to whatever you need).
.
Sub del_skip_rows_repeat_caller()
Call del_skip_rows_repeat(1, 10)
End Sub
Sub del_skip_rows_repeat(nrows As Integer, nrep As Integer)
Dim i As Integer
For i = 1 To nrep
Call del_skip_rows(nrows)
Next i
End Sub
Sub del_skip_rows(nrows As Integer)
Dim rng As Range
Set rng = Range(Selection, Selection.Offset(nrows - 1, 0)).EntireRow
'rng.Select
rng.Delete Shift:=xlUp
Set rng = Selection.Offset(1, 0).Cells(1, 1)
rng.Select
End Sub

Combine multiple columns into one cell with header information

I am trying to create a cell in my worksheet that summarizes all the data in the row. I have found a way to combine all the fields into the first column, but I also need it to include the header name and it needs to ignore fields that are blank. (https://stackoverflow.com/a/13074838)
My excel file has over 50 columns and 5000 rows so I think this might be a job for a macro. Any ideas on how to accomplish this?
╔═════════╦══════╦═════╦═══════════╦═══════╗
║ Summary ║ Name ║ Age ║ County ║ ZIP ║
╠═════════╬══════╬═════╬═══════════╬═══════╣
║ ║ Sue ║ ║ Snohomish ║ 98144 ║
║ ║ Bob ║ 36 ║ Pierce ║ 98335 ║
║ ║ Pat ║ 32 ║ Spokane ║ ║
║ ║ Jim ║ 40 ║ King ║ 98101 ║
╚═════════╩══════╩═════╩═══════════╩═══════╝
Cell A2 would have the following contents:
Name: Sue
County: Snohomish
ZIP: 98144
Cell A3:
Name: Bob
Age: 36
County: Pierce
ZIP 98335
Cell A4:
Name: Pat
Age: 32
County: Spokane
What you need is a formula that :
Uses the IF function to test whether each information cell has data (I'm assuming that you will always have the name).
If the cell does, concatenate the character representation of the ASCII code for a carriage return (CHAR(10)) and the data for that cell.
Be sure to turn on wrapping for the formula cell, or the text that is displayed won't show the line breaks.
The resulting formula looks like this.
="Name: " &B1&
IF(C1="","",CHAR(10)&"Age: "&C1)&
IF(D1="","",CHAR(10)&"County: "&D1)&
IF(E1="","",CHAR(10)&"Zip: "&E1)
You could get fancy and indent the data (age, etc.) by the same amount using the LEN function.

Resources