Home » excel » excel – Show content of other cells that have same text before character "-"

excel – Show content of other cells that have same text before character "-"

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have two sheets.

SHEETS 1 contains a column with some SKU’S with this structure:

GM1012-01

GM1012-04

GM1012-06

9431-01

9431-02

etc..

SHEETS 2 will contain these 2 columns

GM1012-01     |  GM1012-04,GM1012-06  

GM1012-04     |  GM1012-01,GM1012-06  

GM1012-06     |  GM1012-01,GM1012-04  

9431-01       |  9431-02

9431-02       |  9431-01

What I need to do is to starts from Sheet 2.
Search if in Sheet 1 are present sku with same code before “-” and then show all of them comma separated, excluding the one in the first column.

It’s two hour I’m trying to do it 🙁
thanks

How to&Answers:

If i understand correct this may help you:

        Option Explicit

    Sub test()

        Dim Lrow1 As Long
        Dim Lrow2 As Long
        Dim str1 As String
        Dim str2 As String
        Dim i As Long
        Dim j As Long
        Dim Counter As Long
        Dim TopNu As Long

            Lrow1 = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
            Lrow2 = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row

            Sheet2.Range("B2" & ":B" & Lrow2).Clear

            For i = 2 To Lrow1
                str1 = Left(Sheet1.Cells(i, "A").Value, InStr(Sheet1.Cells(i, "A").Value, "-") - 1)
                For j = 2 To Lrow2
                    str2 = Left(Sheet2.Cells(j, "A").Value, InStr(Sheet2.Cells(j, "A").Value, "-") - 1)
                    If (str1 = str2) And Sheet1.Cells(i, "A").Value <> Sheet2.Cells(j, "A").Value Then
                        If Sheet2.Cells(j, "B").Value = "" Then
                            Sheet2.Cells(j, "B").Value = Sheet1.Cells(i, "A").Value
                        Else
                            Sheet2.Cells(j, "B").Value = Sheet2.Cells(j, "B").Value & "," & Sheet1.Cells(i, "A").Value
                        End If
                        If Sheet2.Cells(j, "B").Value <> "" Then
                            Counter = (Len(Sheet2.Cells(j, "B").Value) - Len(Replace(Sheet2.Cells(j, "B").Value, ",", ""))) / Len(",")
                            If Counter = 0 Then
                                Sheet2.Cells(j, "C").Value = 1
                            Else
                                Sheet2.Cells(j, "C").Value = Counter + 1
                            End If
                        End If
                    End If
                Next j
            Next i
            Lrow2 = Sheet2.Cells(Sheet2.Rows.Count, "B").End(xlUp).Row
            For i = 2 To Lrow2
                If Sheet2.Cells(i, "C").Value <> "" Then
                    TopNu = Sheet2.Cells(i, "C").Value
                    Do Until TopNu = 0
                        If Sheet2.Cells(i, "D").Value = "" Then
                            Sheet2.Cells(i, "D").Value = TopNu
                        Else: Sheet2.Cells(i, "D").Value = Sheet2.Cells(i, "D").Value & "," & TopNu
                        End If
                        TopNu = TopNu - 1
                    Loop

                End If
            Next i
    End Sub

Results:
Sheet1:

enter image description here

Sheet2:

enter image description here

Make sure that:

  • In Sheet1 you have SKUs in column A starting from A2.
  • In Sheet2 you have SKUs in column A starting from A2.

For farther assistants see images.

Instructions:

  • Open Excel and press First ALT and then F11.
  • Insert and press Module.
  • Delete everything from the module,copy paste the code and press F5.
  • Visit excel and review the results.