InStr启动参数突然从0开始

时间:2014-04-19 09:15:49

标签: excel vba

如果有“ean”这样的单词,我会检查所有单元格。之后,我想用FontStyle粗体在红色字符串中标记“ean”。一切正常,但问题是,在i = 460之后,pos总是0.我没有找到InStr的起始值的限制。

InStr( [start], string, substring, [compare] )

这是我的代码:

 For a = 2 To lastRow
         Text = LCase(Cells(a, 11).Value)
         TextLength = Len(Text)
         If Text Like "* ean[!A-Za-z0-9]*" Then
             korrektur = TextLength Mod 3
             TextLength = TextLength - korrektur
             Iteration = TextLength / 3
                 For i = 1 To Iteration
                    pos = InStr((i * 3), Text, "ean")
                    Cells(a, 11).Select
                          With ActiveCell.Characters(Start:=pos, Length:=3).Font
                             .Color = vbRed
                             .FontStyle = "bold"
                         End With
                     End If
                 Next i 
      Next a

有什么想法吗?

Greets,Yab

1 个答案:

答案 0 :(得分:0)

尝试:

For a = 2 To lastRow
    Text = LCase(Cells(a, 11).Value)
    TextLength = Len(Text)
    If Text Like "* ean[!A-Za-z0-9]*" Then
        korrektur = TextLength Mod 3
        TextLength = TextLength - korrektur
        Iteration = TextLength / 3
            For i = 1 To Iteration
                pos = InStr((i * 3), Text, "ean")
                Cells(a, 11).Select
                With ActiveCell.Characters(Start:=pos, Length:=3).Font
                    .Color = vbRed
                    .FontStyle = "bold"
                End With
            Next i
    End If
Next a

End If位于第二个For-loop