-1

I would like to extract a code from a larger extract of text, the constants I have is the Code will either start with WP or MD and end in a Numeric value and example of the patterns the code can be in are below;

WP0053

WP053

WP_053

WP_0053

WP 053

WP 0053

MDC_308

WP6

WP6.1

MDC_0308

Please see image of expected output below;

enter image description here

Any help would be much appreciated

4

3 回答 3

0

如果您的代码没有空间,那么下面的代码将有所帮助。

子测试()

Data = "A850085_MDC-WP-01003_SRI Phase 2 - Programme Manager - Dionysios Psachoulias"

startpos = InStr(Data, "WP")
If startpos = 0 Then startpos = InStr(Data, "MD")

fisrtNumPos = 0
LastNumPos = 0
For i = startpos To Len(Data)

    If fisrtNumPos = 0 And LastNumPos = 0 Then
        If IsNumeric(Mid(Data, i, 1)) Then
            fisrtNumPos = i
        End If
    Else
        If Not IsNumeric(Mid(Data, i, 1)) Then
            LastNumPos = i
            Exit For
        End If
    End If
Next i

Endpos = LastNumPos - startpos

Debug.Print Mid(Data, startpos, Endpos)

结束子

现在应该可以了。但如果文本包含“MD”后跟“WP”,那么它将仅从 WP 获取代码。

例如:数据=“A850085_WPC-MD-01003_SRI 第 2 阶段 - 项目经理 - Dionysios Psachoulias”

那么结果将是 result="WPC-MD-01003"

于 2015-06-17T10:25:12.030 回答
0

尝试一些类似的东西: -

Dim cell
Dim tmp as string

For each cell in activesheet.columns(1).usedrange.cells
    If InStr(1, cell.Value, "_MDC_", vbTextCompare) > 0 Then
        tmp = Right(cell.Value, Len(cell.Value) - InStr(1, cell.Value, "_MDC_", vbTextCompare))
        tmp = Left(tmp, InStr(1, tmp, " ", vbTextCompare) - 1)
        cell.offset(0,2).value = tmp
    End If
next cell
于 2015-06-17T12:40:30.683 回答
0

公共函数 GetCode(data As String) As String

startpos = InStr(data, "WP")
If startpos = 0 Then startpos = InStr(data, "MD")

fisrtNumPos = 0
For i = startpos To Len(data)

    If fisrtNumPos = 0 And LastNumPos = 0 Then
        If IsNumeric(Mid(data, i, 1)) Then
            fisrtNumPos = i
        End If
    Else
        If Not IsNumeric(Mid(data, i, 1)) Then
            LastNumPos = i
            Exit For
        End If
    End If
Next i

Endpos = LastNumPos - startpos

GetCode = Mid(data, startpos, Endpos)

结束功能

在任何模块中添加此代码并尝试。

于 2015-06-17T12:41:09.340 回答