如何 return 仅在字符串中的逗号后显示文本

How to return only text after a comma in a string

我正在提取活动 window 标题栏中 parens 之间的文本。那部分工作得很好(感谢我之前在这里收到的一些帮助!)。现在我想创建两个单独的宏 - 一个 returns 只有名字,另一个 returns 只有姓氏。

我的活动 window 标题栏看起来像这样:

左边的一些文字 (HENDERSON,TOM) 右边的一些文字 (逗号后没有space)

姓氏宏完美运行。它看起来像这样:

Sub a1LastName()
    'Extract last name of patient from title bar (between parens)
    Dim strPatientName As String
    Dim OpenPosition As Integer '(open paren marker)
    Dim closeposition As Integer '(close paren marker)
    OpenPosition = InStr(ActiveDocument.ActiveWindow.Caption, "(")
    closeposition = InStr(ActiveDocument.ActiveWindow.Caption, ")")
    strPatientName = Mid(ActiveDocument.ActiveWindow.Caption, _
        OpenPosition + 1, closeposition - OpenPosition - 1)
    Dim c As Long
    c = InStr(strPatientName, ",")
    strPatientName = Left(strPatientName, c - 1)
    Selection.TypeText strPatientName
End Sub

第二个宏与第一个宏相同,除了 second-to-last 行代码有 "Right" 而不是 "Left" 指令:

Sub a1FirstName()
    'Extract first name of patient from title bar (between parens)
    Dim strPatientName As String
    Dim OpenPosition As Integer '(open paren marker)
    Dim closeposition As Integer '(close paren marker)
    OpenPosition = InStr(ActiveDocument.ActiveWindow.Caption, "(")
    closeposition = InStr(ActiveDocument.ActiveWindow.Caption, ")")
    strPatientName = Mid(ActiveDocument.ActiveWindow.Caption, _
        OpenPosition + 1, closeposition - OpenPosition - 1)
    Dim c As Long
    c = InStr(strPatientName, ",")
    strPatientName = Right(strPatientName, c - 1)
    Selection.TypeText strPatientName
End Sub

这是我的问题:"first name" 宏总是 returns 姓氏减去前四个字符,然后是名字,而不是简单的名字。

我在 Google 的任何地方都能找到的唯一示例是专门针对 Excel 的。我结合了我的 VBA 手册,它们都提供了与我用于提取字符右侧文本的相似示例。

我做错了什么?

EDIT 好像第一次没看懂。尝试 2:

您想删除逗号右边的所有内容。

Mystr = Left(Mystr,  Instr(Mystr, ","))
Mystr = Mid(Mystr, 2)

第二行使用 mid 获取第一个字符(括号)之后的所有内容


您正在查找 Mid 函数。

http://www.excel-easy.com/vba/string-manipulation.html#mid

像这样使用它:

Debug.Print Mid("abcde", InStr("abcde", "c"))

会return"cde"

我用 Instr 来定位它应该从哪里开始,而不是写一个数字。要在逗号后获取它,请使用:

Debug.Print Mid(Mystring, InStr(Mystring, "," + 1))

Mystring 是您的字符串。我写了 +1,所以它在逗号之后开始。

您可以使用 Split() 从文本的逗号分隔部分创建一个数组,然后访问第一部分或第二部分:

Sub a1LastName()
    Dim strPatientName As String
    strPatientName = ParensContent(ActiveDocument.ActiveWindow.Caption)
    If strPatientName Like "*,*" Then
        Selection.TypeText Trim(Split(strPatientName, ",")(0))
    End If
End Sub


Sub a1FirstName()
    Dim strPatientName As String
    strPatientName = ParensContent(ActiveDocument.ActiveWindow.Caption)
    If strPatientName Like "*,*" Then
        Selection.TypeText Trim(Split(strPatientName, ",")(1))
    End If
End Sub

'Utility function: find and return text enclosed by ()
'   Return empty string if no () found
Function ParensContent(txt) As String
    Dim rv As String, pos As Long, pos2 As Long
    If txt Like "*(*)*" Then
        pos = InStr(1, txt, "(")
        pos2 = InStr(pos, txt, ")")
        rv = Mid(txt, pos + 1, (pos2 - pos) - 1)
    End If
    ParensContent = rv
End Function

似乎每个人都输入了 Split 以获得名称的 first/second 部分。您还可以使用 Split 去掉括号。如果你知道你只会(并且总是)有一个 ().

代码给出了主要思想。您可以使用 Split 获取 String 中不包括 () 的部分,然后再次执行此操作以获取 [=19= 的任一侧].

Sub t()

    Dim str As String
    str = "(Wall,Byron)"

    Dim name_first As String
    Dim name_last As String

    'two splits total
    name_last = Split(Split(str, "(")(1), ",")(0)
    name_first = Split(Split(str, ")")(0), ",")(1)

    'three split option, same code to remove parentheses
    name_last = Split(Split(Split(str, "(")(1), ")")(0), ",")(0)
    name_first = Split(Split(Split(str, "(")(1), ")")(0), ",")(1)


End Sub

上面的代码提供了一个 two Split 和一个 three Split 选项。主要区别在于,三个 Split 变体使用相同的代码来剥离括号,唯一的变化是 , 的哪一侧被抓取。两个拆分选项利用了这样一个事实,即在逗号上拆分会免费删除一个括号。那里的索引稍微复杂一些。