[英]How can I extract a specific Letter(s) and numbers from a text string via VBA Or excel Formula
I would like to extract a combination of text and numbers from a larger string located within a column within excel. 我想从位于excel列内的较大字符串中提取文本和数字的组合。
The constants I have to work with is that each Text string will 我必须使用的常量是每个文本字符串将
For example; 例如;
COB0012 WP0402 Electronic Payments - SME Consultancy
COB0012 WP0402电子支付-中小企业咨询
DCPY708 A850035 WP161 Configuration Manager Core General (Aman Ranjan) A614019 WP0302 SQL 2005 Upgrade Project – WFCopiesChq - Next Stage SUTP016 EPM Training T2DCPY708 A850035 WP161 Configuration Manager核心常规(Aman Ranjan)A614019 WP0302 SQL 2005升级项目– WFCopiesChq-下一阶段SUTP016 EPM培训T2
Output 输出量
COB0012
COB0012
A850035A850035
SUTP016SUTP016
I have knowledge of the standard Left / Right / Mid / Search functions however my data set is vary large so I would like to create something which would automate this process. 我了解标准的向左/向右/中/搜索功能,但是我的数据集很大,所以我想创建一些可以自动执行此过程的东西。 (1000's of rows)
(1000行)
I imagine a UDF function would do the trick but my familarity with UDF's is very basic. 我以为UDF函数可以解决问题,但是我对UDF的了解非常基础。
Any help would be much appreciated. 任何帮助将非常感激。
Consider: 考虑:
Public Function Xtractor(r As Range) As String
Dim CH As String, L As Long
ary = Split(r.Text, " ")
For Each a In ary
L = Len(a)
CH = Left(a, 1)
If L = 7 Then
If CH = "S" Or CH = "A" Or CH = "C" Then
Xtractor = a
Exit Function
End If
End If
Next a
Xtractor = ""
End Function
@Gary's Student
variant but has some difference @Gary's Student
变量,但有一些区别
Public Function Xtractor(r As Range) As String
Dim a, ary
ary = Split(r.Text, " ")
For Each a In ary
If Len(a) = 7 And a Like "[SAC]*" Then
Xtractor = a
Exit Function
End If
Next a
End Function
output 输出
A regex will be very efficient - especially if you combine it with a variant array rather than a range. 正则表达式将非常有效-尤其是将其与变量数组而不是范围结合使用时。
Sub UseaRegex()
MsgBox StrChange("COB0012 WP0402 Electronic Payments - SME Consultancy [I would require COB0012]")
MsgBox StrChange("DCPY708 A850035 WP161 Configuration Manager Core General (Aman Ranjan)")
End Sub
function 功能
Function StrChange(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "[ACS]\w{6}"
If .test(strIn) Then
StrChange = .Execute(strIn)(0)
Else
StrChange = "No match"
End If
End With
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.