如何搜索文本并将值填充到宏中的下一个单元格?

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

我有一个包含长文本的长数据列表。我想通过查找单词(在A列中)来识别文本,并在匹配的下一个单元格中填充值(如果匹配)(B列)。我知道我可以使用公式来实现,但是条件太多。这会使电子表格变慢。如何用宏实现呢?例如:

Column A        |  Column B
---------------- -------------
this is apple   |  apple

this is grape   |  grape

this is banana  |  banana

etc.....
excel excel-vba macros
1个回答
0
投票
Dim LookFor As String
'This will be a placeholder for the word you want to search for

LookFor = "apple"

On Error Resume Next  
   'in case we could not find the word, we need to continue

Range("A:A").Find(LookFor).Select

If Err = 0 Then
    'if there are no errors, that means we found the word

    Activecell.Offset(0, 1).Value = LookFor
    ' put the same word on the next column, same row
Else
    'there was an error, meaning: the word was not found
    MsgBox "Could not find " & LookFor
End If

'we need to cancel our error detection strategy so that 
' new errors will be reported to the user, otherwise, strange things 
' might happen later
On Error Goto 0
© www.soinside.com 2019 - 2024. All rights reserved.