显示在字符“-”之前具有相同文本的其他单元格的内容

时间:2018-10-26 12:15:51

标签: excel excel-formula

我有两张纸。

SHEETS 1包含一个具有以下SKU的列,其结构如下:

GM1012-01

GM1012-04

GM1012-06

9431-01

9431-02

etc..

SHEETS 2将包含这两列

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

我需要做的是从工作表2开始。 搜索工作表1中是否在sku的“-”之前显示了相同的代码,然后将所有逗号分隔,除了第一栏中的内容。

这是我想要做的两个小时:( 谢谢

1 个答案:

答案 0 :(得分:2)

如果我理解正确,这可能会对您有所帮助

        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

结果: Sheet1:

enter image description here

Sheet2:

enter image description here

请确保:

  • 在Sheet1中,A列中的SKU从A2开始。
  • 在Sheet2中,A列中的SKU从A2开始。

更多的助手可以查看图片。

说明:

  • 打开Excel并按First ALT,然后按F11。
  • 插入并按模块。
  • 从模块中删除所有内容,复制粘贴代码并按F5。
  • 访问excel并查看结果。