gpt4 book ai didi

string - Excel VBA函数处理分隔符

转载 作者:行者123 更新时间:2023-12-03 00:32:08 24 4
gpt4 key购买 nike

我需要有关创建 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"

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

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

此外,实际的标题文本是 xml 标记的一部分,稍后用于 mystringName xml 属性。

对于上面的示例,这些应该是生成的 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

希望你能帮助我让这个功能(或其他功能)正常工作。

非常感谢。

最佳答案

如果我理解正确,您的字符串可能有 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 .

关于string - Excel VBA函数处理分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13449279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com