Vba 从句子中提取字母数字
我希望有一个VBA来从G列(一个句子)中提取一个字母数字值 这句话通常是评论。所以它包括字符和数字 该值始终以Vba 从句子中提取字母数字,vba,excel,Vba,Excel,我希望有一个VBA来从G列(一个句子)中提取一个字母数字值 这句话通常是评论。所以它包括字符和数字 该值始终以AI0开始,以0结束。这可能是11到13位的长度。有时注释中提到的数字为AI038537500,有时也为AI038593790000 我调查了几乎所有的网站,但没有发现任何这样的情况。我知道这些公式,左,右,中,但在我的情况下,它不适用 任何线索都是可观的 你能试试吗?我认为它应该完成这项工作,您也应该使用列值来编辑代码,我使用C列中的注释对其进行了测试,而代码将在D列中编写 Opti
AI0
开始,以0
结束。这可能是11到13位的长度。有时注释中提到的数字为AI038537500
,有时也为AI038593790000
我调查了几乎所有的网站,但没有发现任何这样的情况。我知道这些公式,左
,右
,中
,但在我的情况下,它不适用
任何线索都是可观的 你能试试吗?我认为它应该完成这项工作,您也应该使用列值来编辑代码,我使用C列中的注释对其进行了测试,而代码将在D列中编写
Option Explicit
Sub FindValue()
Dim i As Long
Dim lastrow As Long
Dim lFirstChr As Long
Dim lLastChr As Long
Dim CodeName As String
lastrow = activesheet.Range("c" & Rows.Count).End(xlUp).Row
' gets the last row with data in it
For i = 1 To lastrow
' shuffles through all cell in data
lFirstChr = InStr(1, Cells(i, 3), "A10") ' gets the coordinate of the first instance of "A10"
If lFirstChr = 0 Then GoTo NextIteration
lLastChr = InStr(lFirstChr, Cells(i, 3), " ") ' gets the coordinate of the first instansce of space after "A10"
If lLastChr = 0 Then 'if there is no space after A10 then sets lastchr to the lenght of the string
lLastChr = Len(Cells(i, 3))
End If
CodeName = Mid(Cells(i, 3).Value, lFirstChr, lLastChr - lFirstChr) ' extracts the codename from the string value
Range("d" & i).Value = CodeName
Goto NextTteration
NextIteration:
Next i
End Sub
为什么不试试下面的方法呢
Sub findMatches()
Dim strLength As Integer
Dim i As Long
For i = 1 To Rows.Count
Dim AllWords As Variant
AllWords = Split(Cells(i, 7).Value, " ")
For Each Item In AllWords
strLength = Len(Item)
If strLength > 0 And strLength <= 13 And Item Like "A10*?#" Then
Cells(i, 8) = Item
End If
Next
Next i
End Sub
子查找匹配()
Dim strLength为整数
我想我会坚持多久
对于i=1到行。计数
将所有单词作为变体
AllWords=Split(单元格(i,7).Value,“”)
对于AllWords中的每个项目
strLength=Len(项目)
如果strLength>0且strLength您可以尝试以下方法
将以下用户定义的函数放在标准模块上,然后在表单上使用它,如
=GetAlphaNumericCode(A1)
UDF:
Function GetAlphaNumericCode(rng As Range)
Dim Num As Long
Dim RE As Object, Matches As Object
Set RE = CreateObject("VBScript.RegExp")
With RE
.Global = False
.Pattern = "AI\d{9,}0"
End With
If RE.Test(rng.Value) Then
Set Matches = RE.Execute(rng.Value)
GetAlphaNumericCode = Matches(0)
Else
GetAlphaNumericCode = "-"
End If
End Function
您可以使用Instr
查找“A10”第一次出现的位置,然后再次使用它查找该位置后的第一个“空格”。此外,是否会出现字符串A100
或A10299A1000
?你能举一个输出的例子吗?从A103853700
中,是否提取A103853700
?我将发布一张我希望输出的图片。@BruceWayne我已经发布了一张图片。如果不清楚,请让我知道“始终以A1O开头,以0结尾。这可能是11到13位数长”。。。你的例子与此不符。我迷路了。行lLastChr=InStr(lFirstChr,Cells(I,7),“”)中有一个运行时错误“无效的过程调用或参数”。对不起,我编辑了代码,我的代码是“A1O”而不是“A10”,除列中的注释外,您还有其他数据吗?错误仍然存在。请你在代码旁边对解释发表意见好吗。很容易理解,Rushikumar的解决方案似乎要复杂得多,但我意识到问题可能出在lastrow=工作表(“munka1.Range”(“c”)和Rows.Count.End(xlUp)中.Row引用了我所处理的工作表,我将其更改为activesheet。@rushikumar我在那里遇到了一个用户定义类型未定义的错误。@Mikz,更新了代码以考虑您对13个字符的要求,但我与模式不匹配……在我尝试找到正确的匹配项时,您仍然需要对其进行调整@Mikz,您很可能丢失了引用…因此,请转到工具-->引用
,选中Microsoft VBScript正则表达式1.0
@Mikz的复选框,好的…代码的另一个更新…只需将如“A10”
更改为如“A10*?”
应该可以!我添加了引用,代码正在运行。但是我没有找到任何结果。它只是在运行,没有错误。另外,如果在注释中找到,我想在另一列中打印该值。