【问题标题】:How to find similar string or text in a column in Excel?如何在 Excel 的列中查找相似的字符串或文本?
【发布时间】:2018-04-04 22:23:42
【问题描述】:

在 excel 表中,我有两个列 A 和 B(用文本填充)。我需要为 A 中的每个相同值找到 B 中相似的文本部分。 下面的例子:

名为 product_1000.jpg 的图片有共同的“thecat_”,需要在第三列中给出。

如何自动找到C列? (Excel 公式或 VBA)。 注意:我的表有大约 40k 行。

【问题讨论】:

标签: excel vba


【解决方案1】:

感谢 danieltakeshi 评论的链接问题,我找到了自己的答案

Sub SerieNames()

Dim LastRow As Long
LastRow = sheets(1).Range("A" & Rows.Count).End(xlUp).Row

For a = 2 To LastRow
    If Cells(a, 1).Value = Cells(a - 1, 1).Value Then
        For i = 1 To Len(Cells(a - 1, 3))
            If (Left(Cells(a, 2), i) <> Left(Cells(a - 1, 3), i)) Then
            Cells(a, 3).Value = Left(Cells(a - 1, 3), i - 1)
            Exit For
            End If
        Next i
                If Cells(a, 3).Value = "" Then
                Cells(a, 3).Value = Cells(a - 1, 3).Value
                End If

    Else: Cells(a, 3).Value = Cells(a, 2).Value
    End If

Next a

End Sub

此代码在列 C 中放入从左侧开始的最长公共字符串。 之后,我只需要在 C 列中找到每个系列的最后一个结果即可找到我的答案。这可以通过公式来完成,只需计算每个系列的 C 的最短字符串结果。

注意:A 列必须事先排序,否则代码将不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 2011-03-20
    • 2022-12-10
    • 2016-03-07
    • 1970-01-01
    相关资源
    最近更新 更多