简体   繁体   English

VBA Excel宏查找文本并查找下一个相邻的单元格

[英]VBA Excel Macro find text and find next adjacent cell

I'm trying to write a VBA Excel Macro to create an algorithm that will find text and find next adjacent cell to display text in a msgbox for example: 我正在尝试编写VBA Excel宏来创建一种算法,该算法将查找文本并查找下一个相邻单元格以在msgbox中显示文本,例如:

I parsed the sentence "The building has a broken pipe beneath the first floor." 我解析了句子“建筑物在第一层下面的管道坏了”。 into separate cells. 进入独立的单元格 Using this code: 使用此代码:

Sub Module3()
'
' Parse text
'

'
    Selection.TextToColumns Destination:=Range("A2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
        Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1)), TrailingMinusNumbers:=True
End Sub

The sentence is now parsed. 现在,该句子已解析。 Once I select a cell how can I display the adjacent cell in a msgbox 选择单元格后,如何在msgbox中显示相邻的单元格

  1. Find Text “broken” 查找文字“已损坏”
  2. Find text “broken” and adjacent word “pipe” 查找文本“破”和相邻的词“管道”

This will do what you need: 这将满足您的需求:

Sub FindPlusOffset()
    Dim ws As Worksheet
    Dim match As Range
    Dim findMe As String
    Dim findOffset As String

    Set ws = ThisWorkbook.Sheets("Sheet1")
    findMe = "broken"

    Set match = ws.Cells.Find(findMe)
    findOffset = match.Offset(, 1).Value
    MsgBox "The adjacent word to """ & findMe & """ is """ & findOffset & """."
End Sub

You'll probably want to add some error handling in case the word you are looking for isn't found. 您可能需要添加一些错误处理,以防找不到所需的单词。 You can set the ws object to whatever sheet name you are working with. 您可以将ws对象设置为正在使用的任何工作表名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM