簡體   English   中英

從字符串數組中查找字符串與 Excel 中的字符串數組

[英]Lookup string from array of strings against an array of strings in Excel

我在工作簿中有兩張 Excel 工作表。

我在一張紙上有參考表。 我需要查找單元格中是否存在某個字符串(其中有一個句子),並在引用表中查找該字符串的值並寫入它。

這就是我想要做的:( Sheet 1是操作表; Sheet 2是參考表。)

VLOOKUP(FIND{"compare","contrast","x",..},from the sheet 1 column 1),if string exists,the value against that string in sheet 2 column 2 written in sheet 2 column 2)

{"compare","contrast"} are all words in sheet 1 column 1

我想比較 Sheet 2, Column A 中的任何字符串是否與 Sheet Sheet 1, Column A Sheet 2, Column A中的字符串(在句子或字符串數組中)匹配。 然后,如果它們匹配,則應在Sheet 1, Column B生成針對Sheet 2, Column 2中字符串的值。

你能指導我如何為此編寫宏嗎?

更新:

這是 function。

它需要 2 個參數:第一個是要搜索的單元格(工作表 1,A1),第二個是組成參考表的列(工作表 2,A:B)。 它將采用工作表 2 A 中的所有術語,並從中制作一個變體數組詞匯表,其中 A 列是鍵,B 是值。 如果它在單元格中找到其中一個字符串,它將把它放在一個名為 result 的新字符串中。 作為個人選擇,我將 Glossay 設置為 Static,因此如果您同時在多個單元上運行此 function,它會運行得更快,但如果您願意,可以將其更改為 Dim。

所以對於 A1,你會寫:

=FindString(A1,Sheet2!A:B)

這是代碼,請嘗試一下,希望對您有所幫助,或者至少給您一個良好的開端。

Function FindString(ByVal text As String, _
                    ByVal term_list As range) As String

Dim result As String
Dim i As Long
Static glossary As Variant
glossary = range(term_list.Cells(1, 1).End(xlDown), term_list.Cells(1, 2))

For i = 1 To UBound(glossary)
    If InStr(text, glossary(i, 1)) <> 0 Then
       result = (glossary(i, 1) & " = ") & (glossary(i, 2) & vbLf) & result
    End If
Next

If Len(result) <> 0 Then
    result = Left$(result, (Len(result) - 1))
End If

FindString = result

End Function

暫無
暫無

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

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