0

我的工作簿中有两张 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 1, Column A. 然后,如果它们匹配,则针对字符串 in 的值Sheet 2, Column 2应该在Sheet 1, Column B.

你能指导我如何为此编写宏吗?

4

1 回答 1

3

更新:

这是功能。

它需要 2 个参数:第一个是要搜索的单元格(工作表 1,A1),第二个是组成参考表的列(工作表 2,A:B)。它将采用工作表 2 A 中的所有术语,并从中制作一个变体数组词汇表,其中 A 列是键,B 是值。如果它在单元格中找到其中一个字符串,它将把它放在一个名为 result 的新字符串中。作为个人选择,我将词汇表设置为静态,以便在您同时在多个单元格上运行此函数的情况下运行速度更快,但如果您愿意,可以将其更改为 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
于 2011-07-05T13:38:14.750 回答