2017-10-12 106 views
0

我在表單列中有層次結構編碼系統。我希望在該列中查找與該列中的值部分匹配的值。搜索應該從更長的值開始。這裏的樣本:部分匹配子字符串

AME_ASO_010_010_010

AME_ASO_010_010_010_010(長值,搜索開始)

總之我找一些想法尋找匹配,沒有考慮到去年四個地(_010)。 謝謝大家! 任何支持將不勝感激!

隨着dwirony的貢獻,即時通訊嘗試這一點。請有人知道爲什麼給我的對象所需的錯誤(424)。非常感謝! 它在線路出現故障>左(細胞,LEN(單元) - 4).Offset(1)。選擇

Sub main() 
Dim cell As Range 
Dim arr As Variant, arrElem1 As Variant 
Dim rng As Range 
Dim sh1 As Worksheet 

Set sh1 = Sheets("Valeurs") 

    With Worksheets("Valeurs") 
    For Each cell In .Range("E1", .Cells(.Rows.Count, "E").End(xlUp)) 
    Set rng = Range(cell, cell.Offset(0, 12)) 

      arr = Split(Replace(cell.Value, " ", " "), " ") 
      For Each arrElem1 In arr 
      If Len(arrElem1) = 15 Then 

       Left(cell, Len(cell) - 4).Offset(, 1).Select 
       With Selection.Interior 
       .Pattern = xlSolid 
       .PatternColorIndex = xlAutomatic 
       .ThemeColor = xlThemeColorAccent4 
       .TintAndShade = -0.249977111117893 
       .PatternTintAndShade = 0 
       End With 

      End If 
      Next arrElem1 
    Next cell 
End With 

末次

+0

這是helppful? https://stackoverflow.com/questions/15585058/check-if-a-string-contains-another-string – juju

+0

嗨,謝謝!問題是我不知道如何從搜索中刪除最後四個地方(更長的代碼)。 –

+0

@DanielVelez如果使用宏來查找部分匹配,你可以使用'Left'或'InStr' – dwirony

回答

0

嘗試和努力的成功已經來臨! 這裏的代碼,也許會對其他人有用。 主題已關閉!

Sub main() 
Dim i As Long 
Dim cell As Range 
Dim lResult As String 
Dim arr As Variant, arrElem1 As Variant 
Dim rng As Range, rng1 As Range 
Dim sh1 As Worksheet 

    With Worksheets("Valeurs") 
    For Each cell In .Range("E1", .Cells(.Rows.Count, "E").End(xlUp)) 

     arr = Split(Replace(cell.Value, " ", " "), " ") 
      For Each arrElem1 In arr 
      If Len(arrElem1) = 15 Then 
      lResult = Left(arrElem1, Len(arrElem1) - 4) 
       Set rng1 = sh1.Range("E15:E10000") 
       Set Findv = Range("E15:E10000").Cells.Find(What:=lResult, LookAt:=xlWhole, _ 
       After:=Range("E15"), SearchDirection:=xlPrevious) 
       Findv.Offset(0, 1).Select 
        With Selection.Interior 
        .Pattern = xlSolid 
        .PatternColorIndex = xlAutomatic 
        .ThemeColor = xlThemeColorAccent4 
        .TintAndShade = -0.249977111117893 
        .PatternTintAndShade = 0 
        End With 

      End If 
      Next arrElem1 
    Next cell 
    End With 

末次