简体   繁体   English

Excel VBA从单元格中提取文本

[英]Excel VBA Extract Text from a Cell

I have tried looking through Stack Overflow for previous suggestions but haven't found any that have worked. 我曾尝试通过Stack Overflow寻找以前的建议,但没有找到任何可行的方法。

Here is my situation: I am trying to look at a simple Excel sheet which shows someone's name, position, and then their "Role" which is a custom field I am creating. 这是我的情况:我正在尝试查看一个简单的Excel工作表,该工作表显示某人的姓名,职位以及他们的“角色”,这是我正在创建的自定义字段。 Right now, I am looking to just do "Engineers" but will also expand to things like "Admin Assistant" and "Manager". 现在,我希望只做“工程师”,但还将扩展到“管理助手”和“经理”之类的东西。 (The real spreadsheet is about 8100 lines long). (真正的电子表格长约8100行)。

Here is an example of some test data: 这是一些测试数据的示例: 在此处输入图片说明

All I need is to scan through the "Title" column, see if it matches a String (in this case, my test string is engineer), and then to copy the String and the remaining I or II or III or in some cases, IV after it. 我所需要做的就是浏览“标题”列,查看它是否与字符串匹配(在这种情况下,我的测试字符串是工程师),然后复制字符串以及其余的I或II或III,或者在某些情况下,四之后。

I have heard about using a regular expression and have used them in SQL before, but am struggling coming up with what I need. 我听说过使用正则表达式,并且之前在SQL中使用过正则表达式,但是却很难满足我的需要。 Here is my current code where I tried using the MID function: 这是我尝试使用MID函数的当前代码:

Sub GetRole()
' Custom function written to take role out of official title

strRole = "Engineer" ' String to check for
Dim lrow As Integer ' Number of Rows
Dim Role As String ' Role to write into adjacent cell

lrow = Cells(Rows.Count, "B").End(xlUp).Row

For i = lrow To 2 Step -1
    If InStr(1, Cells(i, 2), "Engineer") > 0 Then
        Role = Mid(Cells(i,3)), 1, 5)
    Cells.(i, 3).Value = Role
    End If
Next i

End Sub

But that didn't quite work. 但这并不太奏效。 Any help or advice would be greatly appreciated. 任何帮助或建议,将不胜感激。 I am willing to provide any extra information necessary. 我愿意提供任何必要的额外信息。

You can solve this using Regular Expressions. 您可以使用正则表达式解决此问题。 First you need to enable the reference which you do so by going to Tools > References... and enable Microsoft VBScript Regular Expressions 5.5 首先,您需要转到工具>引用...来启用引用,然后启用Microsoft VBScript Regular Expressions 5.5

参考

Then use the following code to generate your answers 然后使用以下代码生成您的答案

Sub GetRole()
    ' Custom function written to take role out of official title

    ' Uncomment the below if using Early Binding i.e. you enable the reference
    ' Dim ReGex As New RegExp
    ' Comment below line if decide to use Late Binding (i.e. you enable the reference)
    Dim ReGex As Object
    Dim i As Long, lrow As Long ' Number of Rows

    ' Comment the below line if decide to use Late Binding (i.e. you enable the reference)
    Set ReGex = CreateObject("VBScript.RegExp")

    With ReGex
        .Global = True
        .IgnoreCase = True
        .Pattern = "(Engineer\sI*\b)"
    End With

    With ActiveSheet
        lrow = .Cells(.Rows.Count, "B").End(xlUp).Row
        For i = lrow To 2 Step -1
            If ReGex.test(.Cells(i, 2).Value2) Then .Cells(i, 3).Value2 = Trim(ReGex.Execute(Cells(i, 2).Value2)(0))
        Next i
    End With
End Sub

Generates output: 产生输出:

输出量

I think Excel formula will be easier to extend compared to debugging VBA and Regex: 我认为与调试VBA和Regex相比,Excel公式将更易于扩展:

=IF(ISNUMBER(  FIND("Engineer III", E4)), "Engineer III",
 IF(ISNUMBER(  FIND("Engineer II" , E4)), "Engineer II", 
 IF(ISNUMBER(SEARCH("Engineer *I" , E4)), "Engineer I", "")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM