从 <br> 标签之间提取
Extract from between <br> tags
我有以下格式的电子邮件地址,放在 Excel 2007 年的一列中,如下所示:
<td class="Normal">street name1<br>street name 2<br>city, state zipcode<br>country<br>contact no</TD>
有些单元格具有不同的 <br>
标签,如下所示:
<td class="Normal">street name 1<br>city, state postal<br>country</TD>
我可以使用 Excel "text to columns" 函数提取最后两个标签,但是在列中提取时转换不一致,并且需要永远将每一列对齐到正确的位置。
列表中都有“,”来区分街道地址,我可以使用"text-to column' feature to extract all data before ",”,然后在第一个子集上工作以获取数据,如下所示:
<td class="Normal">street name1<br>street name 2<br>city
有没有办法从前两个 <br>
标签之间提取,或者有一个脚本来计算 <br>
标签的数量,然后使用脚本提取每组 <br>
标签在不同的列中,因为有些有一个 <br>
标签,而其他有两个 <br>
标签。
我想这就是您要找的:
Sub Demo()
Dim str() As String, tempStr As String
Dim lastRow As Long, i As Long, colStart As Long, r As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row '-->get last row with data
For r = 1 To lastRow
tempStr = Range("A" & r).Value
colStart = 2
str = Split(tempStr, "<br>") '-->split string on tag <br>
For i = 1 To UBound(str) - 1
Cells(r, colStart) = str(i)
colStart = colStart + 1
Next
Next r
End Sub
参考图片:
EDIT# 1: 根据我们的讨论进行更改
________________________________________________________________________________
Sub Demo()
Dim str() As String, tempStr As String
Dim lastRow As Long, i As Long, colStart As Long, r As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row '-->get last row with data
For r = 1 To lastRow
tempStr = Range("A" & r).Value
colStart = 2
str = Split(tempStr, "<br>") '-->split string on tag <br>
For i = 1 To UBound(str)
If i = UBound(str) Then
'this section will take care of the string with just one <br> tag
If UBound(str) = 1 Then
Cells(r, colStart) = str(1)
End If
Else
Cells(r, colStart) = str(i)
colStart = colStart + 1
End If
Next
Next r
End Sub
我有以下格式的电子邮件地址,放在 Excel 2007 年的一列中,如下所示:
<td class="Normal">street name1<br>street name 2<br>city, state zipcode<br>country<br>contact no</TD>
有些单元格具有不同的 <br>
标签,如下所示:
<td class="Normal">street name 1<br>city, state postal<br>country</TD>
我可以使用 Excel "text to columns" 函数提取最后两个标签,但是在列中提取时转换不一致,并且需要永远将每一列对齐到正确的位置。
列表中都有“,”来区分街道地址,我可以使用"text-to column' feature to extract all data before ",”,然后在第一个子集上工作以获取数据,如下所示:
<td class="Normal">street name1<br>street name 2<br>city
有没有办法从前两个 <br>
标签之间提取,或者有一个脚本来计算 <br>
标签的数量,然后使用脚本提取每组 <br>
标签在不同的列中,因为有些有一个 <br>
标签,而其他有两个 <br>
标签。
我想这就是您要找的:
Sub Demo()
Dim str() As String, tempStr As String
Dim lastRow As Long, i As Long, colStart As Long, r As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row '-->get last row with data
For r = 1 To lastRow
tempStr = Range("A" & r).Value
colStart = 2
str = Split(tempStr, "<br>") '-->split string on tag <br>
For i = 1 To UBound(str) - 1
Cells(r, colStart) = str(i)
colStart = colStart + 1
Next
Next r
End Sub
参考图片:
EDIT# 1: 根据我们的讨论进行更改 ________________________________________________________________________________
Sub Demo()
Dim str() As String, tempStr As String
Dim lastRow As Long, i As Long, colStart As Long, r As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row '-->get last row with data
For r = 1 To lastRow
tempStr = Range("A" & r).Value
colStart = 2
str = Split(tempStr, "<br>") '-->split string on tag <br>
For i = 1 To UBound(str)
If i = UBound(str) Then
'this section will take care of the string with just one <br> tag
If UBound(str) = 1 Then
Cells(r, colStart) = str(1)
End If
Else
Cells(r, colStart) = str(i)
colStart = colStart + 1
End If
Next
Next r
End Sub