简体   繁体   English

使用 VBA 删除单元格中的最后一个字符

[英]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.我需要将这些值作为参数传递到 SQL 查询中。 In order to do so I need add an additional ' at the start so it reads as 'Abc' and remove the last, after m.为此,我需要在开头添加一个额外的 ',使其读作 'Abc' 并删除 m 之后的最后一个。

I have tried Right(C2,LEN(C2)-(LEN(C2)-1)))我试过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.编辑:VBA 代码。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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