remove duplicates but retain the first position in excel vba macro - excel

I am looking to remove duplicate rows but leave the first line
Using vba macros in excel 2010.
This is the initial information
A | B
1. A | 1
2. A | 1
3. A | 1
4. A | 1
5. B | 2
6. B | 2
7. B | 2
after running the macro
A | B
1. A | 1
2. | 1
3. | 1
4. | 1
5. B | 2
6. | 2
7. | 2
Can you help me,please!

Not elegant, but quick and dirty:
Dim iLastRow As Integer
iLastRow = 13
Range("h1:h" & iLastRow).Formula = "=if(countif(a$1:a1,a1)>1,"""",a1)"
Range("a1:a" & iLastRow).Value = Range("h1:h" & iLastRow).Value
Range("h1:h" & iLastRow).Clear

Related

Draw 16 players into 4 groups where no one comes from same earlier group

I got between 16 and 40 players in groups of 4/5 players. meaning 4 to 10 groups.
I want to draw top16 (i got this list already formatted as:)
Name | Former Group (Ex. With 4 Groups)
Player1 | 1
Player2 | 2
Player3 | 3
Player4 | 4
Player5 | 4
Player6 | 3
Player7 | 2
Player8 | 1
Player9 | 2
Player10 | 1
Player11 | 3
Player12 | 4
Player13 | 1
Player14 | 2
Player15 | 4
Player16 | 3
This list i want to put into 4 Groups with a click of a button. Where no one comes from the same earlier group. This group is listed with players getting 1st in their former group first and then 2nd place and so on. So if its 10 groups of 4 my list could look like. As its 10 1st places and 6 2nd places in.
Name | Former Group (Ex. With 10 Groups)
Player1 | 1
Player2 | 2
Player3 | 3
Player4 | 4
Player5 | 5
Player6 | 6
Player7 | 7
Player8 | 8
Player9 | 9
Player10 | 10
Player11 | 3
Player12 | 4
Player13 | 7
Player14 | 5
Player15 | 9
Player16 | 1
I want to draw these top16 players into 4 new groups where they dont come into a group with a player they already played in the first round.
So i thought i would create a function and call that on a button click.
onClick i want to collect these players from
AA6 to AA21 (is there name)
AB6 to AB21 (is there former group number)
run them thru my function.
Private Sub CommandButton11_Click()
ReDim playerNames(0 To 16) As String
ReDim playerGroups(0 To 16) As Integer
For i = 1 To 16
playerNames(i) = Cells(i + 5, 27).Value
playerGroups(i) = Cells(i + 5, 28).Value
Next i
Dim txt As String
Dim txt2 As String
Dim ii As Long
For ii = LBound(playerNames) To UBound(playerNames)
txt = txt & playerNames(ii) & vbCrLf
txt2 = txt2 & playerGroups(ii) & vbCrLf
Next ii
MsgBox txt + txt2
End Sub
How can i create a logic that never gives me groups where players come from the same group ?
and then i want to past these into
AC6->AC9 (GroupA)
AD6->AD9 (GroupB)
AE6->AE9 (GroupC)
AF6->AF9 (GroupD)

I have data stored in excel where I need to sort that data

In excel, I have data divided into
Year Code Class Count
2001 RAI01 LNS 9
2001 RAI01 APRP 4
2001 RAI01 3
2002 RAI01 BPR 3
2002 RAI01 BRK 3
2003 RAI01 URE 3
2003 CFCOLLTXFT APRP 2
2003 CFCOLLTXFT BPR 2
2004 CFCOLLTXFT GRL 2
2004 CFCOLLTXFT HDS 2
2005 RAI HDS 2
where I need to find the top 3 products for that particular customer for that particular year.
The real trick here is to rank each row based on a group.
Your rank is determined by your Count column (Column D).
Your group is determined by your Year and Code (I think) columns (Column A and B respectively).
You can use this gnarly sumproduct() formula to get a rank (Starting at 1) based on the Count for each Group.
So to get a ranking for each Year and Code from 1 to whatever, in a new column next to this data:
=SUMPRODUCT(($A$2:$A$50=A2)*(B2=$B$2:$B$50)*(D2<$D$2:$D$50))+1
And copy that down. Now you can AutoFilter on this to show all rows that have a rank less than 4. You can sort this on Customer, then Year and you should have a nice list of top 3 within each year/code.
Explanation of sumproduct.
Sumproduct goes row by row and applies the math that is defined for each row. When it is done it sums the results.
As an example, take the following worksheet:
+---+---+---+
| | A | B |
+---+---+---+
| 1 | 1 | 1 |
| 2 | 1 | 4 |
| 3 | 2 | 2 |
| 4 | 4 | 1 |
| 5 | 1 | 2 |
+---+---+---+
`=SUMPRODUCT((A1:A5)*(B1:B5))`
This sumproduct will take A1*B1, A2*B2, A3*B3, A4*B4, A5*B5 and then add those five results up to give you a number. That is 1 + 4 + 4 + 4 + 1 = 15
It will also work on conditional/boolean statements returning, for each row/condition a 1 or a 0 (for True and False, which is a "Boolean" value).
As an example, take the following worksheet that holds the type of publication in a library and a count:
+---+----------+---+
| | A | B |
+---+----------+---+
| 1 | Book | 1 |
| 2 | Magazine | 4 |
| 3 | Book | 2 |
| 4 | Comic | 1 |
| 5 | Pamphlet | 2 |
+---+----------+---+
=SUMPRODUCT((A1:A5="Book")*(B1:B5))
This will test to see if A1 is "Book" and return a 1 or 0 then multiple that result by whatever is B1. Then continue for each row in the range up to row 5. The result will 1+0+2+0+0 = 3. There are 3 books in the library (it's not a very big library).
For this answer's sumproduct:
So ($A$2:$A$50=A2) says to return a 1 if A2=A2 or a 0 if A2<>A2. It does that for A2 through A50 comparing it to A2, returning a 1 or a 0.
(B2=$B$2:$B$50) will test each cell B2 through B50 to see if it is equal to B2 and return a 1 or 0 for each test.
The same is true for (D2<$D$2:$D$50) but it's testing to see if the count is less than the current cells count.
So... essentially this is saying "For all the rows 1 through 50, test to find all the other rows that have the same value in Column A and B AND have a count less than this rows count. Count all of those rows up that meet that criteria, and add 1 to it. This is the rank of this row within its group."
Copying this formula has it redetermine that rank for each row allowing you to rank and filter.

Add non-existent numbers with a zero value in Excel

I have lot of sequential data in Excel.
The missing number in the sequence for Column A, should be populated with zero value in column B.
What formula i could use for this?
Data in excel sheet as below:
A | B
0 | 4
1 | 6
4 | 4
6 | 7
Expected output:
A | B
0 | 4
1 | 6
2 | 0
3 | 0
4 | 4
5 | 0
6 | 7
In B1 of your second table
=IFERROR(VLOOKUP(A1,original_table,2,0),0)
and drag down. Replace original_table with a reference to your first table

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))

Shuffle values to a new row two cells at a time

To explain it in the easiest way possible:
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
... needs to look like:
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 7 | 8 |
I've tried using the TRANSPOSE() function, but it doesn't seem to work in this situation.
How can I accomplish this simple task?
In an unused cell to the right use this formula,
=OFFSET($A$1, INT((ROW(1:1)-1)/2), COLUMN(A:A)-1+MOD((ROW(1:1)-1), 2)*2)
Fill right one column and fill down as far as necessary. Your results should resemble the following.
      
You put excel-vba in your tags, so I'll post the vba code for you. I don't know how to do it with simple cell formulas. Hopefully it's configurable enough to get what you want, beyond the simple example you gave:
START_ROW = 1
START_COL = 1
STEP_COL = 2
OUTPUT_ROW = 3
OUTPUT_COL = 10
Row = START_ROW
Col = START_COL
Out_Row = OUTPUT_ROW
While Cells(Row, Col).Value <> ""
While Cells(Row, Col).Value <> ""
For step = 0 To STEP_COL - 1
Cells(Out_Row, OUTPUT_COL + step).Value = Cells(Row, Col + step).Value
Cells(Out_Row, OUTPUT_COL + step).Value = Cells(Row, Col + step).Value
Next step
Out_Row = Out_Row + 1
Col = Col + STEP_COL
Wend
Col = START_COL
Row = Row + 1
Wend

Resources