【问题标题】:How to Search Text and fill the value to next cell in Macro?如何搜索文本并将值填充到宏中的下一个单元格?
【发布时间】:2020-03-01 16:59:50
【问题描述】:

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

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

this is grape   |  grape

this is banana  |  banana

etc.....

【问题讨论】:

  • 所以你想让宏从B列复制单词并粘贴到A列?你能解释一下最初的情况是什么,以及你想让宏做什么。
  • 不是复制而是搜索文本。例如,如果它包含苹果,那么写下我在下一个单元格或我定义的单元格中定义的文本。希望这很清楚。

标签: excel vba


【解决方案1】:
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

【讨论】:

  • 是否可以添加更多条件?我有 12 个字要搜索。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 2017-08-25
  • 1970-01-01
相关资源
最近更新 更多