문제

I want to split cells in excel on last occurence of : (Colon) . For example test:test1:test2 must be split into test2 i.e., last occurence of :.

How can I do this ?

도움이 되었습니까?

해결책

Assuming that your substring after last : is less than 255 characters:

=TRIM(RIGHT(SUBSTITUTE(B2,":",REPT(" ",255)),255))

if your substring after last : could be longer than 255 characters:

=RIGHT(B2,LEN(B2)-MATCH(2,1/(MID(B2,ROW(INDIRECT("1:"&LEN(B2))),1)=":")))

both formulas with array entry (CTRL+SHIFT+ENTER)

다른 팁

If I understand you correctly you do it like this with VBA WrdArray holds all the words and we use UBound to get the last one in the array.

Public Sub Spl()
    Dim WrdArray() As String
    Dim text_string As String
    text_string = Sheet1.Cells(2, 2)
    WrdArray() = Split(text_string, ":")
    x = WrdArray(UBound(WrdArray()))
    Sheet1.Cells(2, 3) = x
End Sub

Thanks to simoco for pointing out that the original function didn't work.

=RIGHT(B2; SEARCH(":";B2;1))

You could also write your own UDF which wraps InstrRev

Public Function InstrReverse(cell As Range, char As String) As Integer

InstrReverse = InstrRev(cell.Value, char)

End Function

Then (assuming your target string is in A1), this will give you the left part

=LEFT(A1,InstrReverse(A1,":")-1)

and this will give you the right part

=RIGHT(A1,LEN(A1)-InstrReverse(A1,":"))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top