简体   繁体   中英

VBA - Find all values on column B for article on column A and place them to a single row

I have a very large Excel document (20k entries) with data similar to this:

ARTICLE     TIME
article 1   1s
article 1   2s
article 2   1s
article 2   2s
article 2   3s
...

I need to extract the unique values from the articles column and place their corresponding time values on a single row, like this:

ARTICLE     TIMES
article 1   1s  2s
article 2   1s  2s  3s
...

I tried with formulas (INDEX, MATCH, FIND etc...) but I didn't get any proper results.

How can I do this using VBA (maybe?) Thanks.

Try the following code in Before Click event:

cnt = 8
For i = 2 To 6
strg = strg & Cells(i, 2)
If Cells(i, 1) <> Cells(i + 1, 1) Then
Cells(cnt, 1) = Cells(i, 1)
Cells(cnt, 2) = strg
strg = ""
cnt = cnt + 1
End If
Next

The time values are populated in a single cell against each Article

Here is a recipe without using VBA

  1. build aggregated TIMES for each article

     ARTICLE TIME SAME TIMES article 1 1s =if(A3=A2;"YES";"") =B2 article 1 2s =if(A3=A2;"YES";"") = if(A3=A2;Concatenate(B3;" ";D2);B3) article 1 3s =if(A4=A3;"YES";"") = if(A4=A3;Concatenate(B4;" ";D3);B4) article 2 1s (extend the formulas down to the end of range) 
  2. Create a copy, replacing the formulas in SAME and TIMES by the result select columns C and D menu -> copy select columns C and D agin menu -> paste special ; values only

  3. Sort the list by "SAME" (this will put all "YES" in a single block)

  4. Remove all SAME="YES" columns

  5. Sort back to ascending list of articles

  6. Remove "TIME" and "YES" columns

Done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM