VBA, search in column for specific character, extract string upto that character

后端 未结 4 549
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 06:18

In a specific column, I want to search for a specific character in cells...say \"(\" or \"/\". Once this character is found in a cell, I want to extract the part from the be

4条回答
  •  清酒与你
    2020-12-17 07:12

    You could use the Split() function. Here is an example:

    Dim text as String
    Dim splt as Variant
    
    text = "Samsung/Dhamal"
    splt = Split(text, "/")
    MsgBox splt(0)
    

    Just do the same for any other character you want to split. More on this on MSDN: http://msdn.microsoft.com/fr-fr/library/6x627e5f%28v=vs.80%29.aspx

    The other (better?) alternative I see would be to use InStr() with Left(). InStr() returns the position of the first match it finds. Then you just have to crop your string. Here is an example:

    Dim text as String
    Dim position as Integer
    
    text = "Samsung/Dhamal"
    position = InStr(text, "/")
    
    If position > 0 then MsgBox Left(text, position)
    

    http://msdn.microsoft.com/fr-fr/library/8460tsh1%28v=vs.80%29.aspx

提交回复
热议问题