There is a .csv file with a large list of products. Some goods are grouped one after another and differ in one field (in fact, they are one and the same product, which has different properties).

ID NAME PRICE DESC 3630 Π»ΠΎΠ΄ΠΊΠ° Адмирал 280 80.0000 св 1 3630 Π»ΠΎΠ΄ΠΊΠ° Адмирал 280 80.0000 св 2 3630 Π»ΠΎΠ΄ΠΊΠ° Адмирал 280 80.0000 св 3 

How to combine such products into one, writing down all the different fields in one form: св 1;св 2;св 3 ?

 ID NAME PRICE DESC 3630 Π»ΠΎΠ΄ΠΊΠ° Адмирал 280 80.0000 св 1;св 2;св 3 

I try to write in a column like this =Π•Π‘Π›Π˜(A2=A1;B101894&"; "&X2;X2) and multiply to all the lines, but I get a warning about a circular reference .. Why?

  • write a simpler macro in vba, which will compare strings by the similarity of the first lines and the divergence of the last. - Denis
  • @Denis I am not familiar with macros - tell me, plz, if you know how - Vasya
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

2 answers 2

The desired result can be achieved in two stages:

  1. We combine (group) products by the NAME field by creating a pivot table

Result of grouping

  1. Using the MergeIfUniq function, we combine fields with a description

Function call ** _ MergeIfUniq _ **

  1. Final result

Final result

  1. The code of the MergeIfUniq function is taken from http://www.planetaexcel.ru/techniques/7/205/ and a condition is added that eliminates duplication of fields with descriptions

     Function MergeIfUniq (TextRange As Range, SearchRange As Range, Condition As String) Dim Delimeter As String, i As Long Delimeter = ", " 'символы-Ρ€Π°Π·Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΠΈ (ΠΌΠΎΠΆΠ½ΠΎ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π½Π° ΠΏΡ€ΠΎΠ±Π΅Π» ΠΈΠ»ΠΈ ; ΠΈ Ρ‚.Π΄.) 'Ссли Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½Ρ‹ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ ΠΈ склСивания Π½Π΅ Ρ€Π°Π²Π½Ρ‹ Π΄Ρ€ΡƒΠ³ Π΄Ρ€ΡƒΠ³Ρƒ - Π²Ρ‹Ρ…ΠΎΠ΄ΠΈΠΌ с ошибкой If SearchRange.Count <> TextRange.Count Then MergeIfUniq = CVErr(xlErrRef) Exit Function End If 'ΠΏΡ€ΠΎΡ…ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ всС ячСйкам, провСряСм условиС ΠΈ собираСм тСкст Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ OutText 'ΠΈΡΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ Π΄ΡƒΠ±Π»ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ OutText повторящихся Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ ячССк For i = 1 To SearchRange.Cells.Count If (SearchRange.Cells(i) Like Condition) And _ (InStr(1, OutText, TextRange.Cells(i), vbTextCompare) = 0) Then _ OutText = OutText & TextRange.Cells(i) & Delimeter Next i 'Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Π±Π΅Π· послСднСго раздСлитСля MergeIfUniq = Left(OutText, Len(OutText) - Len(Delimeter)) End Function 

Ps. Tested on the following data

 ID Name Price Desk 3630 Π»ΠΎΠ΄ΠΊΠ° Адмирал 280 800 000 св 2 3630 Π»ΠΎΠ΄ΠΊΠ° Адмирал 280 800 000 св 3 3631 Π»ΠΎΠ΄ΠΊΠ° Π’Π°ΠΉΡ„ΡƒΠ½ 700 000 св 1 3631 Π»ΠΎΠ΄ΠΊΠ° Π’Π°ΠΉΡ„ΡƒΠ½ 700 000 св 2 3631 Π»ΠΎΠ΄ΠΊΠ° Π’Π°ΠΉΡ„ΡƒΠ½ 700 000 св 3 3631 Π»ΠΎΠ΄ΠΊΠ° Π’Π°ΠΉΡ„ΡƒΠ½ 700 000 св 4 3631 Π»ΠΎΠ΄ΠΊΠ° Π’Π°ΠΉΡ„ΡƒΠ½ 700 000 св 5 3620 Π»ΠΎΠ΄ΠΊΠ° ΠžΠΏΡ‚ΠΈΠΌΠΈΡΡ‚ 400 000 св 99 3620 Π»ΠΎΠ΄ΠΊΠ° ΠžΠΏΡ‚ΠΈΠΌΠΈΡΡ‚ 400 000 св 91 3620 Π»ΠΎΠ΄ΠΊΠ° ΠžΠΏΡ‚ΠΈΠΌΠΈΡΡ‚ 400 000 св 92 3631 Π»ΠΎΠ΄ΠΊΠ° Π’Π°ΠΉΡ„ΡƒΠ½ 700 000 св 5 3631 Π»ΠΎΠ΄ΠΊΠ° Π’Π°ΠΉΡ„ΡƒΠ½ 700 000 св 5 
  • here it is customary to give a complete answer, and references are given only as a supplement / illustration. - aleksandr barakin

Hey. Here's what I got:

  Sub ΠœΠ°ΠΊΡ€ΠΎΡ() start: For Each element In Range("a2:a8") If element.Value = "" Then Exit Sub ElseIf element.Value = element.Offset(1, 0) Then tmp = element.Offset(0, 3).Value & "; " & element.Offset(1, 3) element.Offset(0, 3).Value = tmp Rows(element.Row + 1).Select Selection.Delete GoTo start Else End If Next element End Sub 

If necessary, I can lay out a file with an example.