0

我有很多产品都有描述,包括项目符号,我想将其转换为 html 形式的 excel 文件。

产品描述就像在一个单元格中:

•   Our aim is to be devoted to building a sustainable future. 
•   We are keen to preserve the human being and its environment.
•   It is best for all skin types

我尝试过的一些宏代码如下:

Sub aTest()
    Dim rCell As Range, spl As Variant, i As Long
    
    For Each rCell In Selection
        spl = Split(rCell, Chr(10))
        For i = LBound(spl) To UBound(spl)
            spl(i) = Chr(60) & "li" & Chr(62) & spl(i) & Chr(60) & "/li" & Chr(62)
        Next i
        rCell = Join(spl, Chr(10))
    Next rCell
    Selection.ColumnWidth = 200
    Selection.EntireRow.AutoFit
    Selection.EntireColumn.AutoFit
End Sub

这段代码给了我输出,包括其中的项目符号:

在此处输入图像描述

预期的:

<li> Our aim is to be devoted to building a sustainable future. </li>
<li> We are keen to preserve the human being and its environment.</li>
<li> It is best for all skin types</li>

任何人都可以帮我解决这个问题,因为我对VB一无所知。此外,拥有函数而不是宏会很棒。

谢谢

更新:我将整个内容放在一个单元格中,并希望将其一起转换为 html。那可能吗?

BIODERMA is a brand of NAOS, the world’s number 1 cosmetic company. BIODERMA has been dedicated to skin health for 40 years and has pioneered many breakthroughs in dermatological care. We are a company that provides high-quality skin care products developed by medical professionals with the absolute highest safety standards. 
Key Features:
•   Our aim is to be devoted to building a sustainable future. 
•   We are keen to preserve the human being and its environment.
•   It is best for all skin types
4

2 回答 2

1
Private Function ConvertBulletToHTML(argInput As String) As String
    Dim cleanArr() As String
    cleanArr = Split(argInput, vbLf)
        
    Dim i As Long
    For i = 0 To UBound(cleanArr)
        If Instr(cleanArr(i), Chr(149)) <> 0 Then
              cleanArr(i) = Replace(cleanArr(i), Chr(149), vbNullString)
              cleanArr(i) = "<li>" & Trim(cleanArr(i)) & "</li>"
        End If
    Next i
        
    ConvertBulletToHTML = "<div>" & Join(cleanArr, vbLf) & "</div>"
End Function

这是输出:

<div><li>Our aim is to be devoted to building a sustainable future.</li> 
<li>We are keen to preserve the human being and its environment.</li> 
<li>It is best for all skin types</li></div>

并输出更新的问题:

<div>BIODERMA is a brand of NAOS, the world’s number 1 cosmetic company. BIODERMA has been dedicated to skin health for 40 years and has pioneered many breakthroughs in dermatological care. We are a company that provides high-quality skin care products developed by medical professionals with the absolute highest safety standards.
Key Features:
<li>Our aim is to be devoted to building a sustainable future.</li>
<li>We are keen to preserve the human being and its environment.</li>
<li>It is best for all skin types</li></div>

如果您不想修剪它,请删除 for 循环中的修剪。

注意:vbLf是一样的,Chr(10)所以你可以只使用内置常量。

于 2021-07-16T13:39:19.050 回答
1

尝试:

spli(i) = Replace(spli(i), Chr(149), "")

您必须检查它是否确实Chr(149)存在,如果不是,则将其更改为其他内容。
其他方式 - 假设它是你的第一个字符spli(i)

spli(i) = Right(spli(i), Len(spli(i)) - 1)

这必须在添加 html 标签之前应用。

于 2021-07-16T13:39:38.643 回答