简体   繁体   English

Excel VBA函数处理定界符

[英]Excel VBA function dealing with delimiters

I need help with the creation of an Excel VBA-function. 我需要有关创建Excel VBA函数的帮助。

A string variable " mystring " can have three different formats. 字符串变量“ mystring ”可以具有三种不同的格式。

The first is just a normal text string. 第一个只是普通的文本字符串。 Nothing needed for this case. 在这种情况下,不需要任何操作。 The other two versions are needed to handle tags and href links for a later html output . 需要其他两个版本来处理标记href链接,以便稍后生成html输出

Excel Example: 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"

So, I need to identify if the string contains a myLink or noLink tag and assign the according href to an xml href-attribute . 因此,我需要确定该字符串是否包含myLinknoLink 标记,然后将根据href分配给xml href-attribute For noLink it's more userfriendly to just write the <nolink>-tag and let the function add the href='#' , I believe. 对于noLink来说,只写<nolink>-tag并让函数添加href='#' ,我相信。

I am open to suggestions if there's a better way to set up those delimiters. 如果有更好的方法来设置这些分隔符,我欢迎您提出建议。

Also, the actually headline text is part of the xml-tag and later used for the mystringName xml-attribute. 另外,实际的标题文本是xml-tag的一部分,以后又用于mystringName xml-attribute。

For the above examples these should be the resulting xml tags: 对于以上示例,这些应该是生成的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="#" />

With the help of XSL I can then deal with the different href attributes. 然后,借助XSL,我可以处理不同的href属性。

How I think this could be used inside VBA: 我认为这可以在VBA中使用:

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

I have stumpled over this function I found online: I would need to write the delimiter a bit differently. 我已经在网上找到了这个功能, 很麻烦:我需要对定界符进行一些不同的编写。 "<myLink href=some/link.html>"This is also a headline Maybe this is a good starting point to split the pieces and put them in an array. "<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

Hope you can help me to get this function (or some other) to work. 希望您能帮助我使此功能(或其他功能)正常工作。

Thanks a lot. 非常感谢。

If I understand correctly, your string can potentially have 1 or 3 sections. 如果我理解正确,则您的字符串可能包含1或3个部分。 I would use a single delimiter (of one or more characters) to separate these sections, and drop the single-quotes, as follows: 我将使用单个定界符(一个或多个字符)来分隔这些部分,并删除单引号,如下所示:

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

The function you write can then look like this: 您编写的函数将如下所示:

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

Of course, it would be much simpler and more elegant to use regular expressions, which you can use by adding a reference to Microsoft VBScript Regular Expressions 5.5 from Tools -> References . 当然,使用正则表达式会更加简单和优雅,您可以通过从Tools -> References添加对Microsoft VBScript Regular Expressions 5.5的引用来使用正则表达式。

Why not write a wrapper function to handle the logic: 为什么不编写包装函数来处理逻辑:

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

Then you just call it as follows: 然后,您只需按以下方式调用它:

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

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

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