Home » excel » excel – VBA copy a value afer plus "+"

excel – VBA copy a value afer plus "+"

Posted by: admin May 14, 2020 Leave a comment


I need help with VBA
Actually, I need to copy a value after plus “+” in a sum, for example “=2000+5000”, I need to copy only “5000” and paste in another cell.

I have this code already:

Dim cell As String
Dim aPos As Integer
Dim bPos As String
Dim result As String
Dim ex As String
Dim cPos As String
Dim dPos As String

Range("a1").NumberFormat = "@"
cell = Range("A1")
    aPos = InStr(cell, "+")
    bPos = Right(cell, aPos)
        Range("b2").NumberFormat = "@" '
        Range("B2").Value = bPos
ex = Range("b2")
    cPos = InStr(ex, "+")
    dPos = Left(ex, cPos)
        Range("b3").NumberFormat = "General"
        Range("b3").Value = dPos

End Sub

Thank you.

How to&Answers:

If all your values are 2 operators like 2000+5000 create your own UDF:

Function GET_SECOND_OPERATOR(ByVal vRng As Range) As Double
GET_SECOND_OPERATOR = CDbl(Split(vRng.Formula, "+")(1))
End Function

enter image description here

Try to adapt this to your needs. Right now this UDF only works if selected cell is a single cell. There is no error handling.