InStr - 只查看字符串的第一部分

时间:2017-05-02 22:33:10

标签: vba excel-vba excel

我有一个包含几千行的数据集,我在每行搜索字符串中的特定文本:7-16,7-26,7-36,13414,SHIPP,CONTAI ...等。 (更多如下所示)。

问题在于,有时某些部件号在部件的末尾有“# - ##”而不是在开头,因此宏也将它归类为“容器/ PGSE部件/培训师”不是。

所以我正在寻找的语法只会查看单元格中字符串的前4个字符(仅适用于包含部分编号的字符,而不是单词)。

例如:

如果要查看包含“ 7-26 734372-102”的单元格,我 希望将其归类为“Container / PGSE Part / Trainers” “在第51栏

如果要查看包含“MS3520 7-26 3”的单元格,我希望将其归类为“Container / PGSE Part / Trainers” “在第51栏中,因为它位于字符串的末尾。

Sub PGSE_Container_Trainer()

Dim rw As Integer

Set sht = ActiveWorkbook.ActiveSheet

rw = 2

Do Until sht.Cells(rw, 1) = ""

If InStr(1, Cells(rw, 8).Value, "7-16") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 8).Value, "7-26") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 8).Value, "7-36") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 8).Value, "7-46") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 8).Value, "7-56") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 8).Value, "7-66") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 8).Value, "7-76") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 8).Value, "7-86") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 8).Value, "7-96") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 8).Value, "13414") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 9).Value, "CONTAI") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 9).Value, "CNTNR") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 9).Value, "SHIPP") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
ElseIf InStr(1, Cells(rw, 8).Value, "REN") Then
    sht.Cells(rw, 51) = "Container / PGSE Part/Trainers"
    GoTo LoopSkip
End If

LoopSkip:
rw = rw + 1
Loop

End Sub

2 个答案:

答案 0 :(得分:0)

如果格式始终是“# - ##”,那么请执行以下操作: If Instr(1, String, "-") = 2 then

由于Instr返回位置索引,您可以按编号对其进行分类。

您也可以做以下事情:  if String like "#-########-##" then

like运算符将检查字符串是否满足模式,因此您应该能够使用它来显式定义模式,并查看字符串是否满足给定模式。有关详细信息,请查看以下内容:https://msdn.microsoft.com/en-us/library/office/gg251796.aspx

此外,如果您想将前三个字符显式匹配到子字符串,您可以执行以下操作:

If Left(String, 3) = SubString then

例如:

Dim SearchString as String: SearchString = "1-2-3_IsThisThingOn"
Dim SubString as String: SubString = "1-2-3"

' Returns "1-2-3"
Debug.Print Left(SearchString, 5)

' Returns true
Debug.Print Left(SearchString, 5) = "1-2-3"

SubString = "4-5-6"

'Returns false
Debug.Print Left(SearchString, 5) = "1-2-3"

答案 1 :(得分:0)

您可以使用稍微不同的方法:用If替换多个ElseIfSelect Case

Instr替换为Like,并在末尾添加通配符*以查找前4个字母。例如Cells(rw, 8).Value Like "7-16*"

使用Select CaseLike和外卡*有一个很好的“技巧”,请参阅下面的代码。

注意:尽量避免使用ActiveSheet,而是使用完全有资格的对象,例如Set Sht = Worksheets("Sheet1")

<强>代码

Option Explicit

Sub PGSE_Container_Trainer()

Dim rw As Long
Dim Sht As Worksheet
Dim CVal As Variant

Set Sht = Worksheets("Sheet1") ' it's better to avoid using ActiveSheet

rw = 2
With Sht
    Do Until .Cells(rw, 1) = ""
        CVal = .Cells(rw, 8).Value
        Select Case True '<-- the trick to have the `Like` inside the Case
            Case CVal Like "7-16*", CVal Like "7-26*", CVal Like "7-36*", CVal Like "7-46*", CVal Like "7-56*", _
                    CVal Like "7-66*", CVal Like "7-76*", CVal Like "7-86*", CVal Like "7-96*", CVal Like "13414*"
                .Cells(rw, 51) = "Container / PGSE Part/Trainers"

        End Select

        ' add more select cases to fit your needs here...

        rw = rw + 1
    Loop
End With

End Sub