基本上我有一个循环,在程序的 第八 循环中,我希望程序查看某个单元格是否包含字符串" Y&# 34 ;.如果它包含Y,它应该转到分配的单元格并标记" X"如果不包含,它应该转到相邻的单元格并标记" X"。 但是,无论我如何设置InStr函数,它总是返回标记未找到的字符串并将其标记在未找到的单元格中。无论是否存在" Y" 有人能指出我正确的方向吗?
For i = 1 To 10
If i = 1 Then
rng1.Copy Destination:=rng2
ElseIf i = 2 Then
rng1.Offset(0, 1).Copy Destination:=rng2.Offset(0, 1)
ElseIf i = 3 Then
rng1.Offset(0, 2).Copy Destination:=rng2.Offset(0, 2)
ElseIf i = 4 Then
rng1.Offset(0, 3).Copy Destination:=rng2.Offset(0, 3)
ElseIf i = 5 Then
rng1.Offset(0, 4).Copy Destination:=rng2.Offset(0, 4)
ElseIf i = 6 Then
rng1.Offset(0, 5).Copy Destination:=rng2.Offset(0, 5)
ElseIf i = 7 Then
rng1.Offset(0, 6).Copy Destination:=rng2.Offset(0, 6)
ElseIf i = 8 Then
If InStr(1, cellText, "Y", 1) > 0 Then '<~Problem here
rng2.Offset(0, 7).Value = "X"
Else
rng2.Offset(0, 8).Value = "X"
End If
ElseIf i = 9 Then
rng1.Offset(0, 10).Copy Destination:=rng2.Offset(0, 9)
Else
rng1.Offset(0, 11).Copy Destination:=rng2.Offset(0, 10)
End If
Next i
答案 0 :(得分:1)
这不是直接解决您的主要问题,而是演示如何使用Select Case
来缩短您的代码(并添加一个消息框来检查您的cellText
变量):
For i = 1 To 10
Select Case i
Case 1 To 7
rng1.Offset(0, i - 1).Copy Destination:=rng2.Offset(0, i - 1)
Case 8
MsgBox cellText
If InStr(1, cellText, "Y", 1) > 0 Then '<~Problem here
rng2.Offset(0, 7).Value = "X"
Else
rng2.Offset(0, 8).Value = "X"
End If
Case 9 To 10
rng1.Offset(0, i + 1).Copy Destination:=rng2.Offset(0, i + 1)
End Select
Next i