2012-11-19 68 views
2

我需要創建一個Excel VBA功能的幫助。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屬性。

對於上面的例子,這些應該是生成的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 

我已經stumpled了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 

希望你能幫助我得到這個功能(或其他)的工作。

非常感謝。

回答

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 

當然,使用正則表達式會更簡單和更優雅,您可以通過在Tools -> References中添加對Microsoft VBScript Regular Expressions 5.5的引用來使用正則表達式。

+0

非常感謝你 - 這不正是我想要 – TonyC

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 
+0

這是比我的更好的解決方案。 –