使用字符串中第一个单词的唯一列表

问题描述 投票:0回答:1

我有一个 2 页的电子表格,其中第 1 张是报告,第 2 张是数据。 我想要做的是从数据表 N 列中检索唯一列表,并将其输入到从单元格 B12 开始的报告表中,但我需要基于报告页面上单元格 B2 中的关键字的唯一输出列表(在这种情况下是比利)。

我也希望这样,如果关键字从 Billy 更改为 Horse,它会将唯一列表从 Billy 更改为任何以 horse 开头的字符串。

由于这将由一些非技术用户使用,越简单越好,理想情况下,我可以将 VBA 代码扔到后面,他们不必担心将是理想的

谢谢

excel vba list unique
1个回答
0
投票
Option Explicit
Sub Demo()
    Dim objDic As Object, rngData As Range
    Dim i As Long, sKey As String, sWord As String
    Dim lastRow As Long, arrData
    Dim oSht1 As Worksheet, oSht2 As Worksheet
    Set oSht1 = Sheets("Sheet1")
    Set oSht2 = Sheets("Sheet2")
    sWord = oSht2.Range("B2")
    lastRow = oSht1.Cells(oSht1.Rows.Count, "N").End(xlUp).Row
    Set rngData = oSht1.Range("N1").Resize(lastRow)
    arrData = rngData.Value
    Set objDic = CreateObject("scripting.dictionary")
    For i = LBound(arrData) + 1 To UBound(arrData)
        sKey = arrData(i, 1)
        If StrComp(sWord, Left(sKey, Len(sWord)), vbTextCompare) = 0 Then
            If Not objDic.exists(sKey) Then
                objDic(sKey) = ""
            End If
        End If
    Next i
    lastRow = oSht2.Cells(oSht2.Rows.Count, "B").End(xlUp).Row
    If lastRow > 12 Then oSht2.Range("B12:B" & lastRow).ClearContents
    oSht2.Range("B12").Resize(objDic.Count, 1) = Application.Transpose(objDic.keys)
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.