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

标签 vba excel

我想检查单元格范围内的特定文本。该文本始终在我的文档中,只是它的单元格是可变的(列始终是 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)

我正在寻找的单元格始终包含此文本 Onderhoud 5/7 binnen 4h 但其位置可能会有所不同,这就是为什么我只需要检查它是否包含其中的任何内容。当我找到该单元格时,我需要同一行上的值 I。

欢迎任何建议!

最佳答案

你能不能只搜索子字符串?

Sheet1.Cells.Find("string to find")

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

例如

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

关于vba - Excel VBA - 检查单元格是否包含文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416801/

相关文章:

file - 如何从文件创建 ole 对象 - Ms-access

excel - 如何通过用户定义的标题名称获取单元格值

excel - Excel 数据透视表中的相关性

perl - Gdata包perl问题

excel - CSV 导出 : Preserving leading zeros

regex - 在正则表达式匹配公式中保留尾随零

vba - 从 Outlook 获取中间名首字母

powershell - 如何在VBA中调用PowerShell命令?

excel - Excel 高级筛选器中的排除筛选

regex - Excel VBA - 查找带有通配符的字符串