循环以从Excel VBA中的单个字符串中提取两个不同的字符串

问题描述 投票:0回答:2

我在excel中收到了html文本,我只想从中提取一些文本。

我在单元格A1中有以下文字:

<b>From:</b></p>  </td>  
<td width=760 colspan=10 valign=bottom 
     style='width:380.0pt;padding:0in 0in 0in 0in;  height:9.05pt'>  
<p class=MsoNormal><a href="mailto:[email protected]">LastName, First</a></p>  
</td> </tr>

我想提取“[email protected]”和“LastName,First”,并将它们分别放入单元格B1和C1中。我需要通过多个单元格循环这个,所以我需要考虑字符串的长度不同。

对于更多的上下文,这个以前的thread为我想要做的事情提供了一个良好的基础,但我坚持如何继续,因为我拉的长度和内容不同的字符串。

excel vba string extract
2个回答
1
投票

使用A1中的字符串,在B1中输入:

=LEFT(MID(A1,FIND("mailto:",A1)+7,9999),FIND(CHAR(34),MID(A1,FIND("mailto:",A1)+7,9999))-1)

并在C1中输入:

=LEFT(MID(A1,FIND(B1,A1)+LEN(B1)+2,9999),FIND("<",MID(A1,FIND(B1,A1)+LEN(B1)+2,9999))-1)

例如:

enter image description here


0
投票

也许是这样的。谨防引号(“),我的HTML变量代码没有考虑到它!在HTML代码字符串分隔符中查找始终相同

Dim HTMLarray1() As String
Dim HTMLarray2() As String
Dim HTML As String

HTML = "<b>From:</b></p></td><td width=760 colspan=10" _
& "valign=bottom style='width:380.0pt;padding:0in 0in 0in 0in;" _
& "height:9.05pt'><p class=MsoNormal>" _
& "<a href="mailto:[email protected]">LastName, First" _
& "</a></p></td></tr>"

HTMLarray1 = Split(HTML, "<a href="mailto:")
HTMLarray1 = Split(HTMLarray1(1), "">")
HTMLarray2 = Split(HTMLarray1(1), "</a>")

Dim email As String
Dim name As String
email = HTMLarray1(0)
name = HTMLarray2(0)
© www.soinside.com 2019 - 2024. All rights reserved.