Excel 如何搜索并仅将搜索的单词复制到下一列

Excel 如何搜索并仅将搜索的单词复制到下一列,excel,excel-formula,worksheet-function,vba,Excel,Excel Formula,Worksheet Function,Vba,这段代码是复制单词“小”“中”“大”它是静态的,但我有 B列中使用的单词在D列中搜索,如果找到,则复制单词 在“E”列中 Option Explicit Sub FindSize() Dim c As Range, s, i As Long s = Array("small", "medium", "large") For Each c In Range("B2", Range("B" & Rows.Count).End(xlUp)) For i = LBound(s) To UBou

这段代码是复制单词“小”“中”“大”它是静态的,但我有 B列中使用的单词在D列中搜索,如果找到,则复制单词 在“E”列中

Option Explicit
Sub FindSize()

Dim c As Range, s, i As Long
s = Array("small", "medium", "large")
For Each c In Range("B2", Range("B" & Rows.Count).End(xlUp))
For i = LBound(s) To UBound(s)
If InStr(UCase(c), UCase(s(i))) > 0 Then
c.Offset(, 1) = UCase(s(i))
Exit For
End If
Next i
Next c 
End Sub

我必须同意香蕉先生的观点,简单的公式是更好的解决办法。在我的头顶上,我会使用:

=TRIMIFERRORIFFINDsmall,LOWERB2>0,Small,,&ifferroriffindmedium,LOWERB2>0,Medium 、、&IFERRORIFFINDLAGE、LOWERB2>0、Large、


你可以把它抄下来。该公式搜索每个文本字符串small、medium和large,返回正确的文本,并在末尾留有空格。当B单元格不包含特定字符串时,IFERROR处理find的值结果。然后将结果连接到&运算符,然后修剪额外的空格。希望这能有所帮助。

工作表公式不是编写宏的好办法吗?