Excel 如何删除VBA中文本字符串末尾以数字开头的内容?
因此,当前我的VBA代码如下所示:Excel 如何删除VBA中文本字符串末尾以数字开头的内容?,excel,vba,excel-formula,Excel,Vba,Excel Formula,因此,当前我的VBA代码如下所示: Sub Testing() Dim K As Long Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row For K = 2 To LR Cells(K, 2).Value = StripAfter(Cells(K, 1), "_", 6) Next K End Sub Function StripAfter(ByVal txt As String, ByVal delimiter
Sub Testing()
Dim K As Long
Dim LR As Long
LR = Cells(Rows.Count, 1).End(xlUp).Row
For K = 2 To LR
Cells(K, 2).Value = StripAfter(Cells(K, 1), "_", 6)
Next K
End Sub
Function StripAfter(ByVal txt As String, ByVal delimiter As String, ByVal
occurrence As Long) As String
Dim x As Variant
x = Split(expression:=txt, delimiter:=delimiter, limit:=occurrence + 1)
StripAfter = x(UBound(x))
End Function
我将其链接到一个按钮,该按钮将输出如下数据:
(旁注:A列已粘贴,B列是运行VBA宏后的结果)
有了这个输出,这正是公式的目的,这是伟大的!我的问题是,我不能把我的头绕到这一点上(我是VBA宏新手,试图尽可能地学习),因为B列中的结果,它们都以数字结尾,数字之间有一个X
。我该如何调整代码,使其删除该部分文本?因此,结果如下所示:
从我所寻找的结果与给出的结果相比,您可以看到,#########
在最后被取出。我在VBA之外玩过,发现这是可行的,但它本质上是一个两步过程:
=RIGHT(SUBSTITUTE(A1,"_",CHAR(10),12),LEN(A1)-FIND(CHAR(10),SUBSTITUTE(A1,"_",CHAR(10),12),1)+1)
^^^这将从A1中获取字符串的最后一部分(第一个图像)
^^^(A20是我从上面的公式中使用的单元格,用于获取A1中字符串的最后一部分),这将删除第一个数字之后的任何内容。这也正是我想要的,但我不知道从哪里开始在上面的VBA公式中实现这一点
任何帮助都将不胜感激 我也很难处理这样的公式,所以我使用了正则表达式,在函数中添加了几行。基本上是模式匹配。你的潜艇和以前一样
Function StripAfter(ByVal txt As String, ByVal delimiter As String, ByVal occurrence As Long) As String
Dim x As Variant
x = Split(expression:=txt, delimiter:=delimiter, limit:=occurrence + 1)
StripAfter = x(UBound(x))
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\d+x\d+$" 'match 1+ numbers followed by x followed by 1+ numbers at the end of a string
If .Test(StripAfter) Then StripAfter = .Replace(StripAfter, "") 'if pattern found replace with empty string
End With
End Function
我也很难处理这样的公式,所以我使用了正则表达式,在函数中添加了几行。基本上是模式匹配。你的潜艇和以前一样
Function StripAfter(ByVal txt As String, ByVal delimiter As String, ByVal occurrence As Long) As String
Dim x As Variant
x = Split(expression:=txt, delimiter:=delimiter, limit:=occurrence + 1)
StripAfter = x(UBound(x))
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\d+x\d+$" 'match 1+ numbers followed by x followed by 1+ numbers at the end of a string
If .Test(StripAfter) Then StripAfter = .Replace(StripAfter, "") 'if pattern found replace with empty string
End With
End Function
您也可以尝试:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long, Chr As Long
Dim str As String, NewStr As String
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
str = .Range("A" & i).Value
For Chr = 1 To Len(str)
If Not IsNumeric(Mid(str, Chr, 1)) Then
NewStr = NewStr & Mid(str, Chr, 1)
End If
Next Chr
.Range("B" & i).Value = NewStr
NewStr = ""
Next i
End With
End Sub
您也可以尝试:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long, Chr As Long
Dim str As String, NewStr As String
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
str = .Range("A" & i).Value
For Chr = 1 To Len(str)
If Not IsNumeric(Mid(str, Chr, 1)) Then
NewStr = NewStr & Mid(str, Chr, 1)
End If
Next Chr
.Range("B" & i).Value = NewStr
NewStr = ""
Next i
End With
End Sub
鉴于已经提出了一个正则表达式解决方案,这里有一个很好的例子供进一步阅读。鉴于已经提出了一个正则表达式解决方案,这里有一个很好的例子供进一步阅读。这几乎100%的时间都有效。如果最后是
x
或x
会发生什么情况有没有办法添加或加入?我试过做一个If
Then
语句,但一下子就给了我一个错误。不管我怎么想,我都想出来了!我在.Global=True
下添加了.IgnoreCase=True
,这就成功了。非常感谢你,我真的很感激你,因为我一点也不明白!这几乎在100%的情况下都有效。如果最后是x
或x
会发生什么情况有没有办法添加或加入?我试过做一个If
Then
语句,但一下子就给了我一个错误。不管我怎么想,我都想出来了!我在.Global=True
下添加了.IgnoreCase=True
,这就成功了。非常感谢你,我真的很感激你,因为我一点也不明白!