Same value then increment - excel

Im new to VBA and need help making a macro to basically see if the value is the same in the column and increment in another column. Bit confusing so heres an example:
INBOUND.T28 1
INBOUND.T28 2
INBOUND.T28 3
INBOUND.T28 4
INBOUND.T29 1
INBOUND.T29 2
INBOUND.T29 3
INBOUND.T29 4
INBOUND.T29 5
INBOUND.T30 1
INBOUND.T30 2
INBOUND.T31 1
INBOUND.T31 2
INBOUND.T31 3
Column A is already in the file, I want column B to print as above. Its basically counting the number of same values in A and restarting count when it a new value is found.
Im not really any good with VBA - although I have done Java in the past and based of that I know that I would need to use some kind of IF statement and a counter such as
value = value + 1
and maybe something like this to count?
Range("A1").Value = Range("A1").Value + 1
Please help, I have no idea how to do VBA and learning it as I go :P Thanks! –

Assuming 'INBOUND.T28' is in Column A and the 1 next to it in Column B then this formula should suit in B2:
=IF(A1=A2,B1+1,1)
for the first row of data, if your first row is blank or labels. You can fill this and subsequent Column B rows with VBA with Application.WorksheetFunction.

Related

counting duplicates and assigning label

In Excel suppose I have a table with the following two columns and following data:
ID Value
1 6
1 2
1 1
2 4
3 5
In excel what I would like to do is write the word duplicate in a third column (say result) when the id is duplicate and is not the highest value.
In this example duplicate would be written next to Value(2),ID(1) and Value(1),ID(1). Value(6), ID(1) would not have duplicate written next to it becasue it has the highest value out of all the ID(1)'s.
Is there an excel formula I can use to do this? If not what VBA would I need? In reality this is a large database and there will be more than 3 duplicates.
The result should look like this:
ID Value
1 6
1 2 Duplicate
1 1 Duplicate
2 4
3 5
Not sure if this is correct. But please correct me if I am wrong.
=IF(MIN($A$2:$A$6 = MIN($B$2:$B$6)), "duplicate", "")
This array formula should work (Ctrl+Shift+Enter) to confirm, though if you have lots of data could be rather slow.
=IF(B2=MAX(IF($A$2:$A$6=A2,$B$2:$B$6)),"","Duplicate")
if the duplicates are in column A, the cell B3 could read: (if ID are decreasing)
=if(COUNTIF($A$1:$A2,A3)>0,"Duplicate #" & COUNTIF($A$1:$A3,A3),"")
does this help?

Excel - count uniques in a range based on another field

I have a situation similar to Excel - Counting unique records in a group but with a final twist that's giving me a headache.
This is basically how my data looks:
A B C
--- --- ---
1 5 2
1 6 2
1 5 2
2 7 1
2 7 1
2 7 1
3 8 1
3 8 1
I'm trying to generate the value in column C. I need to count the number of unique values in column B for each different value in column A. Column A is sorted so that all of the values are together.
I've tried this:
=COUNTIFS($A$2:$A$8,$A2, $B$2:$B$1638,"<>"&"")
That gives me a count of the number of values in the group, but not the number of unique values in the group (so, in my example, it would 3, 3, and 2). I've also tried a pretty cool trick I found on this page which counts all of the unique values in the entire column (so in my example, it would be 4 all the way down). I can't figure out how to split the difference.
I've also tried to figure out if it can be done using the IF function, bu I'm coming up dry on that too. Any help here?
Use this array formula in cell D2:
=SUM(--(FREQUENCY( IF( $A$2:$A$9=A2, MATCH($B$2:$B$9,$B$2:$B$9,0)), ROW( $B$2:$B$9)-ROW( $B$2)+1)>0))
Put this in the formula bar and press CTRL + SHIFT + ENTER (instead of just ENTER) to save it as an array formula. Excel with place these brackets { } around your formula.
Then copy it down. It works well, just be aware that array formulas can get very slow if you use thousands of them in a workbook.
I found this following a link to here from the "cool trick" you linked to.
If it works let me know, and don't forget to mark my answer as accepted. Good luck!

Merge two values in if both values are same except blank in EXCEL

I want to merge two cells in excel that have a value like this:
'a b c
1 1 11
1 2 12
1
2
1
2 2 22
'
I don't want to merge the number with a blank...
any help for that? I used concatenate function but it cannot help me ...
You can produce column C using an if statement and the "&" operator.
=IF(LEN(B2)<1,"",A2&B2)
If you want C to be blank in the case where A is blank, then you'll need an or statement also.
Assuming the data are orientated in columns A and B starting at row 1, =IF(OR(ISBLANK(A1), ISBLANK(B1)),"",CONCATENATE(A1,B1)) is one way. Copy this downwards.
The returned value is a string type. A blank string is inserted into rows where either a or b are not given.
=IF(VALUE(A1)=VALUE(B1),CONCATENATE(A1,B1),"")
OR
=IF(A1=B1,CONCATENATE(A1,B1),"")

Sum row based on criteria across multiple columns

I have googled for hours, not being able to find a solution to what I need/want. I have an Excel sheet where I want to sum the values in one column based on the criteria that either one of two columns should have a specific value in it. For instance
A B C
1 4 20 7
2 5 100 3
3 100 21 4
4 15 21 4
5 21 24 8
I want to sum the values in C given that at least one of A and B contains a value of less than or equal to 20. Let us assume that A1:A5 is named A, B1:B5 is named B, and C1:C5 is named C (for simplicity). I have tried:
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
which gives me the rows where both columns match summed twice, and
={SUMPRODUCT(C,((A<=20)*(C<=20)))}
which gives me only the rows where both columns match
So far, I have settled for the solution of adding a column D with the lowest value of A and B, but it bugs me so much that I can't do it with formulas.
Any help would be highly appreciated, so thanks in advance. All I have found when googling is the "multiple criteria for same column" problem.
Thanks. That works. Found another one that works, after I figured out that excel does not treat 1 + 1 = 1 as I learnt in discrete mathematics, but as you say, counts the both the trues. Tried instead with:
{=SUM(IF((A<=20)+(B<=20);C;0))}
But I like yours better.
Your problem that it is "summing twice" in this formula
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
is due to addition turning first TRUE plus the second TRUE into 2. It is not actually summing twice, because for any row, if only one condition is met, it would count that row only once.
The solution is to transform either the 1 or the 2 into a 1, using an IF:
={SUMPRODUCT(C,IF((A<=20)+(C<=20))>0, 1, 0)}
That way, each value in column C would only be counted at max once.
Following this site you could build up your SUMPRODUCT() formula like this:
=SUMPRODUCT(C,SIGN((A<=20)+(C<=20)))
So, instead of a nested IF() you control your or condition with the SIGN()function.
hth
If you plan to use a large set of data then it is best to use the array formula:
{=SUM(IF((A1:A5<=20)+(B1:B5<=20),C1:C5,0))}
Obviously adjust the range to suit the data set, however if the whole of each column is to form part of the formula then you can simply adjust to:
{=SUM(IF((A:A<=20)+(B:B<=20),C:C,0))}
This will perform the calculation on all rows of data within the A, B and C columns. With either example remember to press Ctrl + Shift + Enter in order to trigger the array formula (as opposed to typing the { and }).

Excel Add up columns regardless of row

I have data like this in an excel sheet:
1 2 3
4 5 6
What I am trying to do is come up with a formal that will give me the total of each row, something like this =A+B+C, so I can use this formula on each row...I have plenty of rows, The above is just and example, I have this =A1+B1+C1 is there away in excel to get A1 + B1 + C1 without the row number?
Please help, this would save me lots of time.
I have looked into =ROW to get the row number, can I use the sum function with the row function?
=SUM(A=ROW():B=ROW())
I found a solution to my problem:
=SUM(INDIRECT("A"&ROW()):INDIRECT("C"&ROW()))
This will work
=SUM(INDIRECT("A"&ROW()):INDIRECT("C"&ROW()))

Resources