VBA:将HTML标签插入特定的HTML标签

时间:2020-06-22 10:06:28

标签: html css excel vba

我在excel表中有这个HTML标记:
enter image description here

要在<h4>..</h4>标签中插入不同的标签,就必须这样做:


<!--opening div-->
<div id="ka-accordion" class="panel-group">

<!--this adds the collapse/expand all to the accordion-->
<div class="panel panel-default">
<div class="panel-heading">
<h4><button class="ka-accordion-btn" data-toggle="collapse" data-target=".panel-collapse">expand/collapse all</button></h4>
</div>
</div>

<!--Question 1-->

  <div class="panel panel-default">
    <div class="panel-heading">
      <h4><button class="ka-accordion-btn" data-toggle="collapse" data-target="#collapse1" data-parent="#ka-accordion">
      
    1st question, is what to do?
      
      </button></h4>
    </div>
    <div id="collapse1" class="panel-collapse collapse">
      <div class="panel-collapse-body">
        <p>
        
<p>Answer 1, body text</p>
<ol><li>some ordered list here included</li></ol>

        
        </p>
      </div>
    </div>
  </div>


  <!--You insert <h3>text heading<h/3> in between questions-->

 <!--Question 2-->
 
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4><button class="ka-accordion-btn" data-toggle="collapse" data-target="#collapse2" data-parent="#ka-accordion">
      
Question no. 2.  from what, to law of gravity?
      
      </button></h4>
    </div>
    <div id="collapse2" class="panel-collapse collapse">
      <div class="panel-collapse-body">
        <p>
        
<p>some body text, to answer number 2</p>
<ol><li>some ordered list here</li></ol>

        
        </p>
      </div>
    </div>
  </div>
  

 <!--Question 3-->
 
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4><button class="ka-accordion-btn" data-toggle="collapse" data-target="#collapse3" data-parent="#ka-accordion">
      
another inquiry, this is the 3rd?
      
      </button></h4>
    </div>
    <div id="collapse3" class="panel-collapse collapse">
      <div class="panel-collapse-body">
        <p>
        
<h3>a sub topic with body, responding to topic 3</h3>
<p>law of classroom article 1</p>

        
        </p>
      </div>
    </div>
  </div>


 <!--Question 4-->
 
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4><button class="ka-accordion-btn" data-toggle="collapse" data-target="#collapse4" data-parent="#ka-accordion">
      
gravity b, question 4th h. collidron?
      
      </button></h4>
    </div>
    <div id="collapse4" class="panel-collapse collapse">
      <div class="panel-collapse-body">
        <p>
        
<p>law of classroom article 1, last answer to last question or topic</p>
<p>law of classroom article 1</p>

        
        </p>
      </div>
    </div>
  </div>

到目前为止,已经对此进行了编码,并在未为字符串声明中的“&”定义的(1)对象上出错。 (2)同样也不知道如何在最后一个</p>之后搜索最后一个</h4>结束标记,并在闭合段落标记之后添加F_Last字符串:

Sub InsrtInTags()

Dim A_Start as String
Dim Aa As String
Dim B As String
Dim C As String
Dim D As String
Dim E As String
Dim F As String 
Dim F_Last As String
Dim Qs As String
Dim rng, cell As Range
Dim i As Integer
Dim pattern As String: pattern = "*<h4>*</h4>*"  ' Define the pattern
Set rng = Worksheets("Sheet1").ListObjects("Table1")
'Assign string values
A_Start = "<!--opening div-->" & vbCrLf & "<div id=""ka-accordion"" class=""panel-group"">" & vbCrLf & "<!--Question"
A = "</div></div></div><!--Question"
Aa = "-->" & vbCrLf & "<div class=""panel panel-default"">" & vbCrLf & "<div class=""panel-heading"">"
B = "<button class=""ka-accordion-btn"" data-toggle=""collapse"" data-target=""#collapse"""
C = "data-parent=""#ka-accordion"">"
D = "</button>"
E = "</div>" & vbCrLf & "<div id=""collapse"""
F = """ class=""panel-collapse collapse"">" & vbCrLf & "<div class=""panel-collapse-body"">"
F_Last = "</div>" & vbCrLf & "</div>" & vbCrLf & "</div>"


'loop through the range and search for h4 html tags

For Each cell In rng
    'Test for h4
    If (cell Like pattern = True) Then
    i = i + 1
    Qs = Mid(cell, InStr(1, cell, "<h4>", vbTextCompare) + 4, (InStr(1, cell, "</h4>", vbTextCompare)) - (InStr(1, cell, "<h4>", vbTextCompare) + 4))
    cell = A_Start & i & Aa & "<h4>" & B & i & C & Qs & D & "</h4>" & E & i & F 'uses the A_start which is a unique begining header tag
        If i > 1 Then
            cell = A & i & Aa & "<h4>" & B & i & C & Qs & D & "</h4>" & E & i & F 'uses generic header tag
        End If
        'add F_Last on last instance </p> of last </h4> no code yet
        </p> & F_Last
    
    End If
Next cell

End Sub

问题或h4标头标签可以是4到20个或更多。这就是为什么要包含一个计数器并循环通过的原因。谢谢!

0 个答案:

没有答案