2017-05-02 22 views
0

我有一個包含幾千行的數據集,我正在搜索字符串中特定文本的每一行:7-16,7-26,7-36,13414 ,SHIPP,CONTAI等等。 (其中更多顯示如下)。InStr - 只在字符串的第一部分看看

問題是,有時某些零件編號在零件的末尾有「# - ##」,而不是在開頭,因此宏在將它分類爲「Container/PGSE Part/Trainers」時也會將其分類真的不是。

所以我正在尋找只會查看單元格中字符串的前4個字符的語法(僅適用於包含部件號而不是單詞的字符串)。

例如:

如果分別來看看含有「7-26 734372-102」我希望它歸類爲一個「容器/ PGSE零件/培訓師」在細胞列51

如果分別來看看含有細胞「MS3520 7-26 3」我不會希望它在列51它歸類爲一個「容器/ PGSE零件/培訓師」,因爲它是在字符串的末尾。

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

使用條件'如果InStr(1,Cells(rw,8).Value,「7-16」)= 1 Then'。這將檢查「7-16」是否在開頭。 –

回答

0

您可以使用稍微不同的方法:將多個If s和ElseIf s替換爲Select 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 
+0

完美!謝謝! – Adije

0

如果格式總是「# - ##」,那麼這樣做: If Instr(1, String, "-") = 2 then

由於InStr返回的位置索引,您可以通過數字歸類

您可能。也可以做這樣的事情: if String like "#-########-##" then

類似的運算符將檢查一個字符串是否滿足一個模式,所以你應該能夠使用它來顯式定義一個模式,並且s如果字符串滿足給定的模式。此外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" 
相關問題