簡體   English   中英

Excel公式/ VBA在其他工作表中搜索部分字符串

[英]Excel Formula/VBA to search partial strings in other sheet

我在工作表1的兩個單元格中有名字(例如:B1(吉娜·威廉姆斯)和B2(帕特里克·拉夫特)),相應的銀行對帳單敘述在工作表2(C列)中,例如: ”。

現在,我需要搜索工作表1的單元格B1和B2中所有可用的四個部分文本(即工作表2的整個B列中的“吉娜”,“威廉斯”,“帕特里克”,““夫”)。匹配我需要為匹配的行捕獲相應的列B&D值。

清單1

Column A      Column B            Column C                         Column D
   1        GINA WILLIAMS     OUTPUT (matching col b of sheet2)  OUTPUT (matching col D of sheet2)
   2        PATRICK RAFTER    OUTPUT (matching col b of sheet2)  OUTPUT (matching col D of sheet2) 

表格2

Column A   Column B     Column C                                              Column D
    1     12/7/2015   Deposit from Gina towards rent for connaught place apt        320 

    2     13/7/2015   Deposit from Rafter towards rent for connaught place apt          720

我嘗試使用vlookup,find,match(以及left,right,mid函數)功能。

您可以使用VBA來實現此目的,但是如果您以前沒有做過VBA,則可能不是一個好主意。

當您手動將工作表1中的名稱輸入到每個單元格時,我希望在工作表2中添加另一列。 在此新列的每個單元格中,您可以使用excel功能區>“數據”>“數據工具”>“ DataValidation”選項,為用戶提供所有可以輸入的名稱的下拉列表。

只要您的銀行對帳單不多,此解決方案就可以使用! 如果是這樣,那么您可能想要以不同的方式進行操作。 它也可以解決工作表1上兩個人具有相同的姓氏或姓氏的問題,而且您可能很快就能做到。

完成上述操作后,您只需在工作表1中使用VLOOKUP在工作表2中查找數據即可。

吻。

哈維

我有一個給你。 我已經測試了代碼。 它非常適合我。

但是,不是重復名稱的授予者,這意味着,重復名稱和重復存款不能給出正確的結果。

這里的代碼:

Sub findAndGet()

    Dim sh1, sh2 As Worksheet
    Dim tempRow1, tempRow2 As Integer
    Dim strList() As String
    Dim name As String
    Dim index As Integer

    'Set sheets
    Set sh1 = Sheets("list")
    Set sh2 = Sheets("search")

    'Set the start row of Sheet1
    tempRow1 = 1

    'Loop all row from starRow until blank of column A in Sheet1
    Do While sh1.Range("A" & tempRow1) <> ""

        'Get name
        name = sh1.Range("B" & tempRow1)

        'Split by space
        strList = Split(Trim(name), " ")

        'Set the start row of Sheet2
        tempRow2 = 1

        'Reset flag
        isFound = False

        'Loop all row from startRow until blank of column A in Sheet2
        Do While sh2.Range("A" & tempRow2) <> ""

            For index = LBound(strList) To UBound(strList)

                'If part of name is found.
                If InStr(UCase(sh2.Range("C" & tempRow2)), UCase(strList(index))) > 0 Then

                    'Set true to search flag
                    isFound = True

                    'exit do loop
                    Exit Do

                End If

            Next index

            'Increase row
            tempRow2 = tempRow2 + 1

        Loop

        'If record is found, set output
        If isFound Then

            'set date
            sh1.Range("C" & tempRow1) = sh2.Range("B" & tempRow2)

            'set amount
            sh1.Range("D" & tempRow1) = sh2.Range("D" & tempRow2)

        End If

        'Increase row
        tempRow1 = tempRow1 + 1

    Loop

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM