简体   繁体   中英

Remove last character in a cell using VBA

I have a value in a cell as below

( Abc','def','hij', 'klm',)

I need to pass these values into a SQL query as parameters. In order to do so I need add an additional ' at the start so it reads as 'Abc' and remove the last, after m.

I have tried Right(C2,LEN(C2)-(LEN(C2)-1)))

But above is deleting all my, I need only last, to be deleted

Try below formula

=REPLACE(REPLACE(A1,1,2,"('"),LEN(REPLACE(A1,1,2,"('"))-2,2,"")

在此处输入图像描述

Edit: VBA Codes. If there is no space after first bracket then try below codes.

Sub sSQL()
Dim x As String, z As String

    x = Range("A1")
    z = "('" & Mid(x, 2, Len(x) - 3) & ")"
    MsgBox z

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM