1

我有下面的代码,如果条件匹配,它会用服务填充偏移单元格。然而在这个阶段,它的操作基础是如果字符串出现在单元格中,它将应用值而不是单词是一个完整的单词。例如,我的标准中有“访问”,而“访问者”(在单元格内)将在我的代码中不应该遵守时。请问有人可以帮忙吗?

Option Compare Text
Sub CHECK_CELL_VALUES()
    Dim LASTROW As Long
    Application.ScreenUpdating = False
        With Sheet1
        LASTROW = .Range("A1048576").End(xlUp).Row


            For i = 2 To LASTROW
                If Cells(i, 7).Value Like "*Service*" _
                Or Cells(i, 7).Value Like "*Servicing*" _
                Or Cells(i, 7).Value Like "* Labour*" _
                Or Cells(i, 7).Value Like "* Job*" _
                Or Cells(i, 7).Value Like "* Hire*" _
                Or Cells(i, 7).Value Like "* Visit*" _
                Or Cells(i, 7).Value Like "* Scaffold*" _
                Or Cells(i, 7).Value Like "* Contract*" _
                Or Cells(i, 7).Value Like "* Hour*" _
                Or Cells(i, 7).Value Like "* Month*" _
                Or Cells(i, 7).Value Like "* Quarter*" _
                Or Cells(i, 7).Value Like "* Day*" _
                Or Cells(i, 7).Value Like "* Maintenance*" _
                Or Cells(i, 7).Value Like "* Repair*" _
                Or Cells(i, 7).Value Like "* Survey*" _
                Or Cells(i, 7).Value Like "* Training*" _
                Or Cells(i, 7).Value Like "* Calibration*" _
                Then Cells(i, 7).Offset(0, 46).Value = "Service"
                Debug.Print Cells(i, 7).Address
            Next i

        End With
    Application.ScreenUpdating = True
End Sub
4

2 回答 2

0

我这样做的方法是:a)在要搜索的字符串之前和之后添加一个空格 b)搜索在之前和之后添加空格的字符串。这是一个搜索单词 VISIT 的示例,它将匹配“VISIT”“A VISIT”“VISIT SOMETHING”,但会排除“VISITOR”

InStr(1, " " & UCase(Sheets("Sheet1").Cells(i, 1)) & " ", " VISIT ")
于 2016-06-01T16:45:04.690 回答
-1

尝试改变这个:

If Cells(i, 7).Value Like "*Service*"

对此:

If InStr(Cells(i,7).Value,"Service") > 0

依此类推 - 这将匹配整个字符串"Service"更多信息InStr可以在这里找到

于 2015-09-28T14:43:54.073 回答