Excel VBA - 检查单元格是否包含文本

Cus*_*omX 3 excel vba excel-vba

我想检查某段文本的一系列单元格.这个文本总是在我的文档中,除了它的单元格是可变的(列始终是B).因此,我检查从1:75开始的范围是否有任何单元格包含一段文本,但它似乎不起作用.

Dim FoundRange As Range
Set FoundRange = Cells.Find("5/7 binnen 4h")
Range("I" & EmptyCell + 2).Value = ... (value of cell I on same row as B)
Run Code Online (Sandbox Code Playgroud)

我正在寻找的Cell总是包含这个文本,Onderhoud 5/7 binnen 4h但它的位置可能会有所不同,这就是为什么我只需要检查它是否包含任何一个.当我找到那个单元格时,我需要在同一行上的值I.

欢迎任何建议!

Ste*_*mer 7

你能不能只搜索子串?

Sheet1.Cells.Find("string to find")
Run Code Online (Sandbox Code Playgroud)

将返回包含字符串的范围(如果找不到字符串,则返回任何内容.

例如

Public Sub Macro1()
Dim FoundRange As Range

Set FoundRange = Sheet1.Cells.Find("5/7 binnen 4h")

' display the cell address to the user
MsgBox FoundRange.Address


' put the found value in column i in the same row as the found text in a known location ($C$1 in this case)
Sheet1.Range("$C$1").Value = Sheet1.Cells(FoundRange.Row, 9).Value

' put the found value in four columns to the right in the same row as the found text in a known location ($C$1 in this case)
Sheet1.Range("$C$2").Value = FoundRange.Offset(0, 4).Value

End Sub
Run Code Online (Sandbox Code Playgroud)