通过列迭代find一个特定的值

我试图通过我的Excel电子表格中的空单元格列,以find单词“是”被发现的行。 之后,在单元格D23find特定行中的单词后,我希望它跨过一列到单元格E23,并将该单元格中的值粘贴到单元格B100中。 这是我迄今为止,但它似乎并没有正常工作:

Sub Test3() Dim x As String x = "Yes" ' Dim found As Boolean ' Select first line of data. Range("D4").Select ' Set search variable value. ' Set Boolean variable "found" to false. found = False ' Set Do loop to stop at empty cell. Do Until ActiveCell.Value = x ' Check active cell for search value. If ActiveCell.Value = x Then Range("B100").Value = ActiveCell.Offset(0, 1).Value found = True Exit Do End If ' Step down 1 row from present location. ActiveCell.Offset(1, 0).Select Loop ' Check for found. If found = True Then MsgBox "Value found in cell " & ActiveCell.Address Else MsgBox "Value not found" End If End Sub 

谢谢!

正如@tigeravatar在他的评论中提到的,你最好使用Excel的本地函数,但是,如果你想通过VBA来做到这一点,你可以更容易,更高效地使用Find函数返回一个范围,如果find否则“没有”,如果没有。

使用这个,你可以testing看看你回来了什么。 尝试这个:

 Sub Test3() Dim x As String Dim rng As Range x = "Yes" Set rng = Range("D4:D10000").Find(x) If Not rng Is Nothing Then Range("B100").Value = rng.Offset(0, 1).Value MsgBox "Value found in cell " & rng.Address Else MsgBox "Value not found" End If End Sub 

希望它能做到这一点。