下面的代码有效 - 如果在E列中找到字符串abcd
,那么它的位置将打印在X列的同一行中,如果找不到则会打印0
Sub SearchInColumn()
Dim LastRow As Integer
Dim SrchIn As String
Dim SrchFor As String
LastRow = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
For i = 2 To LastRow
SrchIn = Sheet1.Cells(i, 5).Value
SrchFor = "abcd"
'If SrchFor = "abcd" Then
Sheet1.Cells(i, "X").Value = InStr(SrchIn, SrchFor)
'End If
Next i
End Sub
与if语句相同的代码不能如下所示 - 在X列中没有打印,为什么? if语句有什么问题?
Sub SearchInColumn()
Dim LastRow As Integer
Dim SrchIn As String
Dim SrchFor As String
LastRow = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
For i = 2 To LastRow
SrchIn = Sheet1.Cells(i, 5).Value
'SrchFor = "abcd"
If SrchFor = "abcd" Then
Sheet1.Cells(i, "X").Value = InStr(SrchIn, SrchFor)
End If
Next i
End Sub
答案 0 :(得分:0)
尚未为SrchFor分配值。
答案 1 :(得分:0)
这一行
LastRow = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
始终返回1,以便for never执行
Dim LastRow As Integer
Dim SrchIn As String
Dim SrchFor As String
LastRow = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
For i = 2 To 4
'In the next line you search for the value, but then in the next you use an if to check if 'SrchFor has something but the variable with the value is SrchIn not SrchFor and this point SrchFor 'has nothing. Thats why the if don't work
SrchIn = Sheet1.Cells(i, 5).Value
'SrchFor = "abcd"
If SrchFor = "abcd" Then
Sheet1.Cells(i, "X").Value = InStr(SrchIn, SrchFor)
End If
Next i
答案 2 :(得分:0)
您的代码无效,因为If SrchFor = "abcd" Then
行始终评估为FALSE
,因为SrchFor
始终为0
。一些提示
Long
,否则可能会遇到OverFlow错误。 Rows.Count
不是完全限定的。您可能希望看到THIS 代码A
Sub SearchInColumn()
Dim LastRow As Long, i As Long
Dim SrchIn As String, SrchFor As String
Dim ws As Worksheet
Set ws = [Sheet1]
SrchFor = "abcd"
With ws
LastRow = .Range("E" & .Rows.Count).End(xlUp).Row
For i = 2 To LastRow
SrchIn = .Cells(i, 5).Value
If InStr(1, SrchIn, SrchFor) Then
.Cells(i, "X").Value = InStr(1, SrchIn, SrchFor)
End If
Next i
End With
End Sub
代码B
或者,您可以使用.FIND/.FINDNEXT而不是使用LookAt:=
作为xlPart
进行循环
此处还有非VBA解决方案
将此公式粘贴到单元格X1中并将其拉下来。
=IF(ISERROR(SEARCH("abcd",E1,1)),"",SEARCH("abcd",E1,1))
答案 3 :(得分:0)
查看您尝试搜索文本的两种方式 第一种方式:
SrchFor = "abcd"
Sheet1.Cells(i, "X").Value = InStr(SrchIn, SrchFor)
因为已为SrchFor分配了一个值,所以系统知道要搜索的内容。
秒路失败:
If SrchFor = "abcd" Then
Sheet1.Cells(i, "X").Value = InStr(SrchIn, SrchFor)
因为计算机不知道SrchFor到达if
时的值是什么,所以当它检查它是否等于“abcd”时,它将未知与 abcd 并且该比较失败,因此它不会在if
之后执行代码。
为了允许扩展,并允许它能够搜索许多字符串,我们需要将SrchFor
扩展为值列表(数组),然后搜索数组中的每个项目< / p>
这会使函数看起来像这样:
Option Explicit
Sub SearchInColumn()
Dim LastRow As Long
Dim i As Long
Dim SrchIn As String
Dim SrchFor(2) As String '2 for 2 strings
Dim SearchForIndex As Long
Dim FoundPos As Long
SrchFor(1) = "abcd"
SrchFor(2) = "NewString"
LastRow = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
For i = 2 To LastRow
SrchIn = Sheet1.Cells(i, 5).Value
For SearchForIndex = 1 To 2
'use same number as you put into the array definition
FoundPos = InStr(SrchIn, SrchFor(SearchForIndex))
If FoundPos > 0 Then Exit For
Next
Sheet1.Cells(i, "X").Value = FoundPos
Next i
End Sub
一旦给出任何字符串,这将立即终止,并返回该字符串的位置
此外,请注意使用Option Explicit
- 这会强制您定义每个变量 - 当我设法将SearchForIndex
拼写为SerchForIndex
时,它非常有用,而不会出现奇怪的结果