Get a Sum of ONLY duplicate values - excel

If there a way to use a sumif combined with a countif?
for example if I have
TransactionNo|NumberToSum
1 |
1 | 9
2 | 6
3 | 3
4 |
4 | 4
5 | 6
6 | 4
7 |
7 | 9
8 | 7
9 | 4
10 |
10 | 1
11 | 3
12 | 6
My result would be: 23
because the values 1,4,7,10 are duplicated, so I need to sum the NumberToSum of those 9,4,9,1 in this case.
Ideally I would like somethings along the lines of =SumIf(A:A,Count>1,B:B)
Where B is summed if the count of A inside of A:A is > 1.
but =SUMIF(A:A,COUNT(A:A)>1,B:B) does not work.
NOTE: the Data is NOT sorted as it appears in my example.
If I wanted to do it in VBA one quick way could be:
Sub sumdups()
Dim rng As Range
Dim rng2 As Range
Dim Sum As Long
Set rng = [A2:A17]
For Each rng2 In rng
If WorksheetFunction.CountIf(Range("A1:A17"), rng2.Value) > 1 Then
Sum = Sum + rng2.Offset(0, 1).Value
End If
Next rng2
End Sub
Another option I have tried is to add a column with the formula
=IF(COUNTIF(A:A,A2)>1,B2,"")
Then sum the helper column, but I am hoping there is a simple formula that can do the same all in one cell.
I am really just looking for a faster formula that can be entered in a single cell, without the need for any additional calculations.

This formula works for me:
=SUMPRODUCT(1*(COUNTIF(A1:A100,A1:A100)>1),(B1:B100))
or for entire column (a little bit slower):
=SUMPRODUCT(1*(COUNTIF(A:A,A:A)>1),(B:B))

Related

If I specify hours and a number of days, then copy the hours into the number of days

I'm trying to do something that seems rather simple, but has some issues in execution.
Let's say I have a specific number of hours and a number of days input. I then want to flow those number of hours into each of the number of days on a separate sheet, i.e., 5 days if 5 is input, 3 days if 3 is input, so on and so forth.
Sheet A | Sheet B
|hours|days| | |monday|tuesday|wednesday|thursday|friday|saturday|sunday|
+--+-----+-+ | ++--++--++--++--++--++--++--++--++--++--+-++--++--++--++-+
| 8 | 5 | | | 8 | 8 | 8 | 8 | 8 | 0 | 0 |
Any input would be invaluable!
If you have this in SheetA
then this in SheetB. Put this formula in A2 and copy across
=IF(COLUMNS($A2:A2)>SheetA!$B$2,0,SheetA!$A$2)
The COLUMNS formula will increase 1,2, etc as you drag the formula across so once it has gone 6 columns along the formula will return zero.
Use a macro with a shortcut or button with this code
Dim hours, days, Week as Range
Set hours = sheets(“YourSheetName”).range(“CellWithHours”)
Set days = sheets(“YourSheetName”).range(“CellWithDays”)
Set Week = sheets(“YourSheetName”).range(range(“CellWithMonday”), Range(“CellWithSunday”))
For i = 1 to days.value
Week.Cells(i).value = hours.value
Next i
For i = 1 to 7
If Week.Cells(i).value = “” then
Week.Cells(i).value = 0
End if
Next i

Coding string to numeric in Excel

I want to convert string codes to integer codes
Here is my Excel file
Level1 Level2 Level3 Level4 Time1 Time2 Time3 Time4
A B E C 12 14 12 13
D D E C 11 17 16 9
E C C B 14 13 19 8
C E B D 11 12 10 7
B A A D 10 11 7 6
A= 4, B=3, C=2, D=1 , E= 0
get this in sheet2
Level1 Level2 Level3 Level4 Time1 Time2 Time3 Time4
4 3 0 2 12 14 12 13
1 1 0 2 11 17 16 9
0 2 2 3 14 13 19 8
2 0 3 1 11 12 10 7
3 4 4 1 10 11 7 6
Is it possible?
Use the ASCII table and some basic maths.
dim i as long
for i=65 to 69
worksheets("sheet1").cells.replace what:=chr(i), replacement:=(69-i)
next i
This isn't as fast as a dictionary, but its clocking at 1 second for 10,000 rows across 4 columns.
Searching in columns A - D down to last row (determined by Col A)
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim SearchRange As Range: Set SearchRange = ws.Range("A2:D" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
Application.ScreenUpdating = False
SearchRange.Replace What:="A", Replacement:=4, LookAt:=xlWhole
SearchRange.Replace What:="B", Replacement:=3, LookAt:=xlWhole
SearchRange.Replace What:="C", Replacement:=2, LookAt:=xlWhole
SearchRange.Replace What:="D", Replacement:=1, LookAt:=xlWhole
SearchRange.Replace What:="E", Replacement:=0, LookAt:=xlWhole
Application.ScreenUpdating = True
Set up an area of any worksheet (for example Sheet2) with your chosen mapping of letters to numbers, like this:
+--------+-------+
| Letter | Value |
+--------+-------+
| A | 4 |
| B | 3 |
| C | 2 |
| D | 1 |
| E | 0 |
+--------+-------+
Then, suppose on one of your main worksheets you have a letter in cell B3="D". You can enter a formula into a different cell, perhaps cell C3, with your lookup formula: =VLOOKUP(B3,Sheet2!A2:B10,2,FALSE). (The range A2:B10 in this case would be replaced with the range of your entire letter-to-number table). The formula will return the number value.
Enter this in Sheet2!A2
=IF(ISNUMBER(Sheet1!A2),Sheet1!A2,69-CODE(Sheet1!A2))
Fill over/down as needed.

Excel array formula to find row of largest values based on multiple criteria

Column: A | B | C | D
Row 1: Variable | Margin | Sales | Index
Row 2: banana | 2 | 20 | 1
Row 3: apple | 5 | 10 | 2
Row 4: apple | 10 | 20 | 3
Row 5: apple | 10 | 10 | 4
Row 6: banana | 10 | 15 | 5
Row 7: apple | 10 | 15 | 6
"Variable" sits in column A, row 1.
"Fruit" refers to A2:A6
"Margin" refers to B2:B6
"Sales" refers to C2:C6
"Index" refers to D2:D6
Question:
From the above table, I would like to find the row of two largest "Sales" values when Fruit = "apple" and Margin >= 10. The correct answer would be values from row 3 and 6. I have tried the following methods without success.
I have tried
=LARGE(IF(Fruit="apple",IF(Margin>=10,Sales)),{1,2}) + CSE
and this returns 20 and 15, but not the row.
I have tried
=MATCH(LARGE(IF(Fruit="apple",IF(Margin>=10,sales)),{1,2}),Sales,0)+1
but returns row 2 and 6 as the first matches to come up are the 20 and 15 from "banana" not "apple".
I have tried
=INDEX(D2:D7,LARGE(IF(Fruit="apple",IF(Margin>=10,ROW(Sales)-ROW(INDEX(Sales,1,1))+1)),{1,2}),1)
But this returns row 7 and 5 (i.e. "Index" 6 and 4) as these are just the first occurrences of "apple" starting from the bottom of the table. They are not the largest values.
Can this be done with an Excel formula or do would I need a macro? If macro, can I please get help with the macro? Thank you!
use this formula:
=INDEX(D:D,AGGREGATE(15,6,ROW($A$2:$A$7)/(($B$2:$B$7>=10)*($A$2:$A$7="apple")*($C$2:$C$7 = AGGREGATE(14,6,$C$2:$C$7/(($B$2:$B$7>=10)*($A$2:$A$7="apple")),F2))),1))
I put 1 and 2 in F2 and F3 respectively to find the first and second.
Edit #1
to deal with duplicates we need to add (COUNTIF($G$1:G1,$D$2:$D$7) = 0). The $G$1:G1 needs to refer to the cell directly above the first placement of this formula. So the formula needs to start in at least row 2.
=INDEX(D:D,AGGREGATE(15,6,ROW($A$2:$A$7)/((COUNTIF($G$1:G1,$D$2:$D$7) = 0)*($B$2:$B$7>=10)*($A$2:$A$7="apple")*($C$2:$C$7 = AGGREGATE(14,6,$C$2:$C$7/(($B$2:$B$7>=10)*($A$2:$A$7="apple")),F2))),1))

Sum odd cell values if even cells are not empty

I have the following data:
4 | | 1 | 2 | | | 2 | 3
I want to sum odd values when the following even cell is not empty.
So my sum must be: 1 + 2 = 3
I use this formula:
=SUMPRODUCT((MOD(COLUMN(F2:BE2);2)=0)*F2:BE2)
It obviously gives: 4 + 1 + 2 = 7
How can I change the formula to get the 1st result?
Try this:
=SUMPRODUCT((MOD(COLUMN(E2:BD2);2)=1)*(F2:BE2<>"")*(E2:BD2))

Excel to calculate if values in ranges meet certain criteria using VBA

I have two ranges in excel, say:
x | y
------------
5 | -1
46 | -4
2 | 1
67 | -1
22 | 1
6 | 0
34 | 0
7 | -2
I want calculate the sum of the second column for values less than O only if the respective values in the first column is less than 10 (i.e sum(y(i) for i<0 and x(i)<10) . Hence in this case the sum will be -3.
Assuming your headers are in A1:B1 and your data is A2:B9 use this:
=SUMIFS(B2:B9,A2:A9,"<10",B2:B9,"<0")
Try something like
Function calc() AS Integer
Dim sum AS Integer: sum = 0
Dim c AS Range
For Each c In ThisWorkbook.Worksheets(1).Range("A1:A15")
If c.Value < 10 And c.Offset(0, 1).Value < 0 Then
sum = sum + c.Offset(0, 1).Value
End If
Next c
calc = sum
End Function

Resources