如何从单元格中提取特定字符?

时间:2019-07-14 15:23:33

标签: excel vba

尝试提取相同类型的字符,然后将其从一个单元格移动到另一个单独的单元格。

  

走廊11.2-27.6室外。

  

中间走廊612-476返回室外。

收件人:

  • CELL(1):走廊-室外
  • CELL(1):中间走廊-后室外
  • CELL(2):11.2
  • 手机(3):27.6

尝试了一些正则表达式和变量,但没有运气。无法理解逻辑。

预期结果是:

enter image description here

走廊-室外11.2 27.6

中间走廊-户外612476

5 个答案:

答案 0 :(得分:0)

尝试以下简短宏:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

enter image description here

EDIT#1:

此版本可以在测量前后处理多个单词:

Sub parseit()
    Dim cell As Range

    For Each cell In Selection
        arr = Split(cell.Text, " ")
        cell.Offset(0, 1).Value = arr(0) & " - " & arr(2)
        brr = Split(arr(1), "-")
        cell.Offset(0, 2).Value = brr(0)
        cell.Offset(0, 3).Value = brr(1)
    Next cell
End Sub

enter image description here

答案 1 :(得分:0)

如果将Sub ParseIt2_The_Sequel() Dim cell As Range, special As String For Each cell In Selection special = "" arr = Split(cell.Text, " ") For i = LBound(arr) To UBound(arr) If InStr(arr(i), "-") > 0 Then special = arr(i) arr(i) = "-" End If Next i cell.Offset(0, 1).Value = Join(arr, " ") brr = Split(special, "-") cell.Offset(0, 2).Value = brr(0) cell.Offset(0, 3).Value = brr(1) Next cell End Sub 放在单元格 A2 中,则可以使用以下公式获得your outcome

单元格 B2 Hallway 11.2-27.6 Outdoor

单元格 C2 =SUBSTITUTE(SUBSTITUTE(A2,C2,""),D2,"")

单元格 D2 =SUBSTITUTE(LEFT(SUBSTITUTE(MID(A2,FIND(" ",A2,1)+1,20),"-",REPT("!",30)),20),"!","")

这是在找到仪表板周围的区域,然后插入一堆!标记,然后删除。如果您突破每个步骤,您都可以遵循。

这是c和d公式的另外两个选项

=SUBSTITUTE(LEFT(SUBSTITUTE(MID(A2,FIND("-",A2,1)+1,99)," ",REPT("!",30)),20),"!","") =MID(A3,FIND(" ",A3,1)+1,FIND("-",A3,1)-FIND(" ",A3,1)-1)+0

答案 2 :(得分:0)

如果要提取数字并使用正则表达式将剩下的所有数相加,则可以执行以下操作:

Sub ExtractValues(ByVal inputString As String, _
                  ByRef targetCell1 As Range, _
                  ByRef targetCell2 As Range, _
                  ByRef targetCell3 As Range)
    Dim regEx As New RegExp
    With regEx
        .Global = True
        .Pattern = "^(.+\s)(\d+(?:\.\d+))?-(\d+(?:\.\d+))(\s.+)$"
    End With

    Dim matches As MatchCollection
    Set matches = regEx.Execute(inputString)
    If matches.Count = 1 Then
        Dim m As Match: Set m = matches(0)
        targetCell1.Value = m.SubMatches(0) + "-" + m.SubMatches(3)
        targetCell2.Value = m.SubMatches(1)
        targetCell3.Value = m.SubMatches(2)
    End If
End Sub

用法:

Sub Test()
    ExtractValues Range("A1").Value, Range("E1"), Range("F1"), Range("G1")
End Sub

结果:

Result

答案 3 :(得分:0)

尝试此代码

Sub Test()
Dim a, x, i&

a = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).Resize(, 3).Value
For i = LBound(a) To UBound(a)
    x = Split(a(i, 1))

    a(i, 1) = x(0) & " - " & x(UBound(x))
    a(i, 2) = Split(x(1), "-")(0)
    a(i, 3) = Split(x(1), "-")(1)
Next i
Range("F2").Resize(UBound(a, 1), UBound(a, 2)).Value = a
End Sub

答案 4 :(得分:0)

如果您具有带有TEXTJOIN函数的Excel 2016+,则可以将以下 array 公式用于位置:

=TEXTJOIN(" ",TRUE,FILTERXML("<t><s>" &SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(REPLACE($A2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},$A2&"0123456789")),0,CHAR(160)&CHAR(32)),"-"," "))," ","</s><s>"),CHAR(160),"-")&"</s></t>","//s[not(number())]"))

由于这是一个数组公式,因此您需要在按下 enter 的同时按住 ctrl + shift 来“确认”它。如果操作正确,Excel将按照公式栏中的说明在公式周围放置括号{...}

对于两次测量,请使用(常规公式):

=FILTERXML("<t><s>"&SUBSTITUTE(TRIM(SUBSTITUTE($A2,"-"," "))," ","</s><s>")& "</s></t>","//s[number()>0][1]")

=FILTERXML("<t><s>"&SUBSTITUTE(TRIM(SUBSTITUTE($A2,"-"," "))," ","</s><s>")& "</s></t>","//s[number()>0][2]")

enter image description here

算法:

  • 从原始字符串中删除连字符
  • 在第一个数字前插入一些未使用的字符(我使用了NBSP)
  • 用空格替换连字符
  • 用连字符替换NBSP(以连字第一个和最后一个文本子字符串
  • 使用空格作为节点标记来创建XML文档
  • 返回并加入非数字节点作为位置
  • 返回测量的第一和第二数字节点

编辑

如果您在字符串的开头和结尾出现的字数有限(+连字符),并且您没有TEXTJOIN函数,但是确实有Excel 2013 FILTERXML函数,可以使用以下串联(正常输入)。

您将需要重复此操作:

IFERROR(FILTERXML("<t><s>" &SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(REPLACE($A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},$A1&"0123456789")),0,CHAR(160)&CHAR(32)),"-"," "))," ","</s><s>"),CHAR(160),"-")&"</s></t>","//s[not(number())][1]")&" ","")

每个可能的单词。请注意[1]。每个非数字单词都会递增。

因此,对于您的数据,假设最多要连接5个单词+连字符,则可以使用以下公式:

=TRIM(CONCATENATE(
IFERROR(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(REPLACE($A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},$A1&"0123456789")),0,CHAR(160)&CHAR(32)),"-"," "))," ","</s><s>"),CHAR(160),"-")&"</s></t>","//s[not(number())][1]")&" ",""),
IFERROR(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(REPLACE($A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},$A1&"0123456789")),0,CHAR(160)&CHAR(32)),"-"," "))," ","</s><s>"),CHAR(160),"-")&"</s></t>","//s[not(number())][2]")&" ",""),
IFERROR(FILTERXML("<t><s>" &SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(REPLACE($A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},$A1&"0123456789")),0,CHAR(160)&CHAR(32)),"-"," "))," ","</s><s>"),CHAR(160),"-")&"</s></t>","//s[not(number())][3]")&" ",""),
IFERROR(FILTERXML("<t><s>" &SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(REPLACE($A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},$A1&"0123456789")),0,CHAR(160)&CHAR(32)),"-"," "))," ","</s><s>"),CHAR(160),"-")&"</s></t>","//s[not(number())][4]")&" ",""),
IFERROR(FILTERXML("<t><s>" &SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(REPLACE($A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},$A1&"0123456789")),0,CHAR(160)&CHAR(32)),"-"," "))," ","</s><s>"),CHAR(160),"-")&"</s></t>","//s[not(number())][5]")&" ",""),
IFERROR(FILTERXML("<t><s>" &SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(REPLACE($A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},$A1&"0123456789")),0,CHAR(160)&CHAR(32)),"-"," "))," ","</s><s>"),CHAR(160),"-")&"</s></t>","//s[not(number())][6]")&" ","")))

使用2013年公式的结果

enter image description here