首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于添加HTML标记的Excel VBA

用于添加HTML标记的Excel VBA
EN

Stack Overflow用户
提问于 2016-10-17 12:49:33
回答 1查看 1.7K关注 0票数 0

我有下面的代码,用于为单元格追加文本。但是它并没有附加HTML标记。

例如:

9/23/99 10:37我们不去了。我身体不好。哥伦布不是一个繁荣的地方。大多是退役军人。她打电话来问为什么她是唯一的一个。也想要她的搭档。我解释说在会议上会发生这种事。9/10/99 4:15 P Rick提到995美元想要埃塞克斯基金公司WY 4262-3607-0011-9582 8/01 8/23/99 9:08am想要一家NV公司。需要做一点财政上的改变。几个节目。穆和导师。帕特斯和我.我60岁了。我昨天把牌用完了。

应该就像

"HTML段落标签“9/23/99 10:37我们不去。我身体不好。哥伦布不是一个繁荣的地方。大多是退役军人。她打电话来问为什么她是唯一的一个。也想要她的搭档。我解释说在会议上会发生这种事。“结束HTML段落标签”"HTML段落标签“9/10/99无需担心

下面是我得到的代码

代码语言:javascript
复制
Option Explicit

Sub main()
    Dim newStrng As String
    Dim word As Variant
    Dim strngToBeAppended As String

    strngToBeAppended = Application.InputBox("Input string to be appended", 1)

    With Worksheets("TextSheet") '<-- change "TextSheet" to your actual sheet with text name
        For Each word In Split(.Range("A1").Text, " ") '<-- assuming that the text to be splitted is in cell "A1" of the referenced worksheet
            If Len(word) - Len(Replace(word, "/", "")) = 2 Then
                newStrng = newStrng & " " & strngToBeAppended & word
            Else
                newStrng = newStrng & " " & word
            End If
        Next word
        .Range("A2").Value = LTrim(newStrng)
    End With
End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-17 12:57:34

试试这个:

代码语言:javascript
复制
Option Explicit

Sub main()
    Dim newStrng As String
    Dim word As Variant
    Dim parTag As String, endParTag As String
    Dim dateCounter As Long

    parTag = "<p>" '<-- change this to whatever your "HTML Paragraph Tag" may be
    endParTag = "</p>" '<-- change this to whatever your "End of HTML Paragraph Tag" may be
    With Worksheets("TextSheet") '<-- change "TextSheet" to your actual sheet with text name
        For Each word In Split(.Range("A1").Text, " ") '<-- assuming that the text to be splitted is in cell "A1" of the referenced worksheet
            If Len(word) - Len(Replace(word, "/", "")) = 2 Then
                dateCounter = dateCounter + 1
                If dateCounter > 1 Then newStrng = newStrng & endParTag
                newStrng = newStrng & parTag & word
            Else
                newStrng = newStrng & " " & word
            End If
        Next word
        If dateCounter > 1 Then newStrng = newStrng & endParTag
        .Range("A2").Value = LTrim(newStrng)
    End With
End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40086837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档