VBA Like运算符不会拾取某些字符串

时间:2014-08-07 13:18:22

标签: excel vba

我正在编写一些代码来搜索每个单元格中的某些字符串。我所拥有的代码将与我尝试的每个字符串完美配合,但由于某种原因不会选择我正在尝试搜索的网址。这是代码:

If Cells(datanum, myDescCol).Value Like "*Design Review*" Or Cells(datanum, myDescCol).Value Like "*Design Review Protocol*" Or Cells(datanum, myDescCol).Value Like "*Review Protocol*" Then
    If Worksheets("CS-CRM Raw Data").Cells(datanum, myDescCol).Value Like "*https://extern.website.com/xyz/og/inu/dis/*" Then
        Worksheets("Request Results").Cells(reportnum, 7).Value = "Yes"
    ElseIf Cells(datanum, myDescCol).Value Like "*Not Required*" Then
        Worksheets("Request Results").Cells(reportnum, 7).Value = "No"
        Worksheets("Request Results").Cells(reportnum, 7).Interior.Color = 65535
    Else
        Worksheets("Request Results").Cells(reportnum, 7).Value = "No"
    End If
Else
    Worksheets("Request Results").Cells(reportnum, 7).Value = "No"
End If

我有各种各样的语句,它们都可以正常工作,但是对于Else或ElseIf,网站的If语句通常会被跳过,即使单元格有字符串。通常在单元格中,网站看起来像https://extern.website.com/xyz/og/inu/dis/5586574657465836574385,但由于数字不同,我在dis /后切断了搜索。知道为什么它没被拿起来吗?

编辑:我还应该在编辑网址时添加,以便删除数字,将https://extern.website.com/xyz/og/inu/dis/留在单元格中,但代码仍然无法获取。

1 个答案:

答案 0 :(得分:1)

使用InStr功能会有效吗?

段:

If InStr(1, Worksheets("CS-CRM Raw Data").Cells(datanum, myDescCol).Value, "https://extern.website.com/xyz/og/inu/dis/") Then

全:

If Cells(datanum, myDescCol).Value Like "*Design Review*" Or Cells(datanum, myDescCol).Value Like "*Design Review Protocol*" Or Cells(datanum, myDescCol).Value Like "*Review Protocol*" Then
    If InStr(1, Worksheets("CS-CRM Raw Data").Cells(datanum, myDescCol).Value, "https://extern.website.com/xyz/og/inu/dis/") Then
        Worksheets("Request Results").Cells(reportnum, 7).Value = "Yes"
    ElseIf Cells(datanum, myDescCol).Value Like "*Not Required*" Then
        Worksheets("Request Results").Cells(reportnum, 7).Value = "No"
        Worksheets("Request Results").Cells(reportnum, 7).Interior.Color = 65535
    Else
        Worksheets("Request Results").Cells(reportnum, 7).Value = "No"
    End If
Else
    Worksheets("Request Results").Cells(reportnum, 7).Value = "No"
End If