Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除最后一个字符串后缀vba_Vba_Excel - Fatal编程技术网

删除最后一个字符串后缀vba

删除最后一个字符串后缀vba,vba,excel,Vba,Excel,我想知道是否有更有效的方法来删除单元格值中的最后一个字符串值 在我的情况下,我需要删除一个后缀,即 TNV1602000014C //delete suffix C 117239B //delete suffix B MRV117299 //nothing to delete 115365Z //delete suffix Z 我试过这种方法 lastrow = Cells(Rows.Count,

我想知道是否有更有效的方法来删除单元格值中的最后一个字符串值

在我的情况下,我需要删除一个后缀,即

TNV1602000014C      //delete suffix C 
117239B             //delete suffix B
MRV117299           //nothing to delete
115365Z             //delete suffix Z        
我试过这种方法

lastrow = Cells(Rows.Count, "C").End(xlUp).Row  
Range("E2:E" & lastrow).Formula = "=TRIM(RIGHT(SUBSTITUTE(RC[-1],""A"",REPT("""",LEN(RC[-1]))),LEN(RC[-1])))"
但是因为我需要为整个A-Z做这件事。
有什么办法吗?

下面将循环任何选定的单元格,如果最后一个字符是字母(不是数字或特殊字符),则单元格中的最后一个字符将被删除

 Sub RemoveTrailingLetter()
    Dim c As Range

    'Replace Selection With Your Range
    For Each c In Selection
        'If The Last Character Is A Letter
        If IsLetter(Right(c, 1)) Then
            'Remove The Last Character
            c = Left(c, Len(c) - 1)
        End If
    Next c

End Sub

Function IsLetter(strValue As String) As Boolean
Dim intPos As Integer

    For intPos = 1 To Len(strValue)
        Select Case Asc(Mid(strValue, intPos, 1))
            Case 65 To 90, 97 To 122
                IsLetter = True
            Case Else
                IsLetter = False
                Exit For
        End Select
    Next

End Function

将其粘贴到您需要使用的工作簿的模块中。
它将删除所有非数字后缀(您可能要删除多个字符)。
您可以直接在Excel中使用它,如
=RemoveSuffix(D1)
或在VBA循环中使用

Public Function RemoveSuffix(ByVal StringToClean As String) As String
    Dim i As Integer

    If IsNumeric(Right(StringToClean, 1)) Then
        RemoveSuffix = Trim(StringToClean)
    Else
        i = 1
        Do While Not IsNumeric(Mid(StringToClean, Len(StringToClean) - i, 1))
            i = i + 1
        Loop
        RemoveSuffix = Trim(Left(StringToClean, Len(StringToClean) - i))
    End If
End Function
填写E栏:

lastrow = Cells(Rows.Count, "C").End(xlUp).Row  
Range("E2:E" & lastrow).FormulaR1C1 = "=RemoveSuffix(RC[-1])"
或覆盖D列:

lastrow = Cells(Rows.Count, "C").End(xlUp).Row
for i = 2 to lastrow  
    Cells(i,4).Value = RemoveSuffix(Cells(i,4).Value)
next i

一种稍微简单的公式方法:

lastRow = Cells(Rows.Count, "C").End(xlUp).Row
Range("E2:E" & lastRow).FormulaR1C1 = "=left(RC[-1],LEN(RC[-1])-ISERROR(RIGHT(RC[-1])+0))"

如果您愿意,您也可以使用
Evaluate
就地执行。

为什么不使用
not IsNumeric(…)
而不是为此创建一个函数?@R3uK允许使用标点符号,我不相信原始海报needed@JiminyCricket:不,如果
IsNumeric
,则不认为是数字。。。顺便说一句,您应该使用
AscW
而不是
Asc
,因为特殊字符将返回一个几乎是随机数的
Asc
,因为它不适合使用的格式!;)很好,罗瑞,非常好nice@brettdj:像往常一样^^加德。。谢谢更简单更干净D对此表示感谢:D@Anne:不客气!如果您要删除多个字符,这将非常有用,但如果您只需删除一个字符,Rory的公式非常简洁!;)我也会把它保存在我的代码圣经中:D谢谢你的努力!:D