Excel VBA函数处理分隔符

时间:2012-11-19 07:21:24

标签: string excel vba delimiter

我需要帮助创建Excel VBA函数。

字符串变量“ mystring ”可以有三种不同的格式。

第一个只是一个普通的文本字符串。这种情况不需要任何东西。 其他两个版本需要处理标记 href链接以用于以后的 html输出

Excel示例:

mystring = "This is a headline"
mystring = "<myLink href='some/link.html'</myLink> This is also a headline"
mystring = "<noLink href='#'</noLink> This is another headline"

因此,我需要确定该字符串是否包含 myLink noLink 标记并指定相应的 href 到xml href-attribute 。 对于noLink,我认为只需编写<nolink>-tag并让函数添加href='#'对用户更友好。

如果有更好的方法来设置这些分隔符,我愿意接受建议。

此外,实际标题文字是xml标记的一部分,后来用于 mystringName xml-attribute。

对于上面的示例,这些应该是生成的xml标记:

<mytag mystringName="This is a headline"/>
<mytag mystringName="This is also a headline" href="some/link.html" />
<mytag mystringName="This is another headline" href="#" />

XSL 的帮助下,我可以处理不同的href属性。

我认为这可以在VBA中使用:

If mystring = "myLink" Then
xmf.writeline "<mytag mystringName=""" & mystring & """href= """ & href & """ > """
End If

我在网上找到的this函数已经弄乱了: 我需要稍微改写分隔符。    "<myLink href=some/link.html>"This is also a headline 也许这是分割碎片并将它们放在一个数组中的一个很好的起点。

Public Function GetStringFromQuotation(ByRef sText, sDelimiter As String)
     'Store the position of the 1st and 2nd delimiter in the String
    Dim iPositionOfFirstDelimiter As Integer, iPositionOfSecondDelimiter As Integer
     'Store the length of the delimiter
    Dim iLenDelimiter As Integer

     'Deliver nothing if the function doesn't get a single usable parameter
     'otherwise you'd get an error later on
    If Len(sText) = 0 And Len(sDelimiter) = 0 Then
        GetStringFromQuotation = ""
        Exit Function
    End If

    iLenDelimiter = Len(sDelimiter)
     'Find 1st occurence of delimiter
    iPositionOfFirstDelimiter = InStr(sText, sDelimiter)
     'Find the 2nd one, starting right behind the first one
    iPositionOfSecondDelimiter = InStr(iPositionOfFirstDelimiter + iLenDelimiter, _
    sText, sDelimiter)

     'If there are 2 occurences
    If iPositionOfFirstDelimiter > 0 And iPositionOfSecondDelimiter > 0 Then
         'Take the part of the string that's right between them
        GetStringFromQuotation = Mid(sText, iPositionOfFirstDelimiter + iLenDelimiter,     _
        iPositionOfSecondDelimiter - iPositionOfFirstDelimiter - iLenDelimiter)
    Else
        GetStringFromQuotation = ""
    End If
End Function

希望你能帮助我实现这个功能(或其他功能)。

非常感谢。

2 个答案:

答案 0 :(得分:4)

如果我理解正确,你的字符串可能有1或3个部分。我将使用单个分隔符(一个或多个字符)来分隔这些部分,并删除单引号,如下所示:

'Using a semicolon
mystring = "This is a headline"
mystring = "myLink;some/link.html;This is also a headline"
mystring = "noLink;#;This is another headline"

您编写的函数可能如下所示:

Public Function GetXML(str As String) As String
Dim mylinkPos As Integer, nolinkPos As Integer
Dim remainderString As String, nextDelimiterPos As String
Dim href As String, headline As String

mylinkPos = InStr(str, "mylink;")
nolinkPos = InStr(str, "nolink;")

If mylinkPos = 0 And nolinkPos = 0 Then
    GetXML = "<mytag mystringName=""" & str & """ />"
    Exit Function
End If

remainderString = Mid(str, 8)
If nolinkPos > 0 Then
    headline = remainderString
    href = "#"
Else
    nextDelimiterPos = InStr(remainderString, ";")
    href = Left(remainderString, nextDelimiterPos - 1)
    headline = Mid(remainderString, nextDelimiterPos + 1)
End If

GetXML = "<mytag mystringName=""" & headline & """ href=""" & href & """ />"
End Function

当然,使用正则表达式会更加简单和优雅,您可以通过在Microsoft VBScript Regular Expressions 5.5添加对Tools -> References的引用来使用它。

答案 1 :(得分:2)

为什么不写一个包装函数来处理逻辑:

Private Function makeLink(linkText As String, Optional linkUrl As String)
Dim linkUrlText As String
If (Len(Trim(linkUrl)) > 0) Then
    linkUrlText = " href=" & """" & linkUrl & """"
End If
makeLink = "<mytag mystringName=""" & linkText & """" & linkUrlText & " />"
End Function

然后你只需按如下方式调用它:

Sub test()

Dim link1 As String

link1 = makeLink("This is a headline")
link2 = makeLink("This is also a headline", "some/link.html")
link3 = makeLink("This is another headline", "#")

End Sub