我想我和InStr有点问题。
我有一个for循环,循环遍历一堆值,并在单元格包含"--“时正确退出。
但是,如果单元格包含可以在Range("A2")中找到的文本,那么我希望它将其值输出到Range("A5")。
Private Sub CommandButton21_Click()
Dim cell As Range
For Each cell In Sheets(1).Range("$B:$B")
Dim i As Long
i = cell.Row + i
If InStr(1, cell.Text, Range("A2").Text, vbTextCompare) Then Range("A5").Value = cell.Text
If cell.Text = "--" Then Exit For
Next cell
End Sub不知道为什么这种比较会失败。
发布于 2015-02-26 08:36:48
InStr返回一个与搜索字段中搜索字符串的起始位置相等的数值,因此您必须将其与一个数字进行比较:
If InStr(1, cell.Text, Range("A2").Text, vbTextCompare) > 0 Then Range("A5").Value = cell.Text顺便说一句,如果你不需要它,那么处理一个.Value2比处理一个.Text更快-参见this question。
https://stackoverflow.com/questions/28731640
复制相似问题