[英]How can I extract a specific Letter(s) and numbers from a text string via VBA Or excel Formula
我想從位於excel列內的較大字符串中提取文本和數字的組合。
我必須使用的常量是每個文本字符串將
例如;
COB0012 WP0402電子支付-中小企業咨詢
DCPY708 A850035 WP161 Configuration Manager核心常規(Aman Ranjan)A614019 WP0302 SQL 2005升級項目– WFCopiesChq-下一階段SUTP016 EPM培訓T2
輸出量
COB0012
A850035
SUTP016
我了解標准的向左/向右/中/搜索功能,但是我的數據集很大,所以我想創建一些可以自動執行此過程的東西。 (1000行)
我以為UDF函數可以解決問題,但是我對UDF的了解非常基礎。
任何幫助將非常感激。
考慮:
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
變量,但有一些區別
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
輸出
正則表達式將非常有效-尤其是將其與變量數組而不是范圍結合使用時。
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 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.