Làm cách nào để chuyển đổi HTML sang Excel bằng mã VBA?

Như ảnh chụp màn hình bên dưới được hiển thị, nếu số lượng thẻ html tồn tại trong các ô trang tính của bạn, làm cách nào bạn có thể chuyển đổi chúng thành văn bản thuần túy trong Excel?

Làm cách nào để chuyển đổi HTML sang Excel bằng mã VBA?



Chuyển đổi html thành văn bản trong các ô có chức năng Tìm và Thay thế

Bạn có thể chuyển đổi tất cả html thành văn bản trong ô bằng chức năng Tìm và Thay thế trong Excel. Vui lòng làm như sau

1. Chọn các ô bạn sẽ chuyển đổi tất cả html thành văn bản và nhấn phím Ctrl + F để mở hộp thoại Tìm và Thay thế

2. In the Find and Replace dialog box, go to the Replace tab, enter <*> into the Find what box, keep the Replace with box empty, and click the Replace All button. See screenshot:

Làm cách nào để chuyển đổi HTML sang Excel bằng mã VBA?

3. Sau đó, hộp thoại Microsoft Excel bật lên cho bạn biết có bao nhiêu thẻ html đã được thay thế, nhấp vào nút OK và đóng hộp thoại Tìm và Thay thế

Làm cách nào để chuyển đổi HTML sang Excel bằng mã VBA?

Sau đó, bạn có thể thấy tất cả các thẻ html bị xóa khỏi các ô đã chọn như ảnh chụp màn hình bên dưới được hiển thị

Làm cách nào để chuyển đổi HTML sang Excel bằng mã VBA?


Chuyển đổi html thành văn bản trong toàn bộ trang tính bằng VBA

Ngoài ra, bạn có thể chuyển đổi tất cả html thành văn bản trong toàn bộ trang tính cùng lúc với mã VBA bên dưới

1. Mở bảng tính chứa html bạn sẽ chuyển đổi thành văn bản, sau đó nhấn tổ hợp phím Alt + F11 để mở cửa sổ Microsoft Visual Basic for Applications

2. Trong cửa sổ Microsoft Visual Basic for Applications, nhấp vào Chèn > Mô-đun, sau đó sao chép mã VBA bên dưới vào cửa sổ Mô-đun

mã VBA. Chuyển đổi html thành văn bản trong toàn bộ trang tính

Sub RemoveHTMLTags()
'Update by Extendoffice 20180703
    Dim xRg As Range
    Dim xCell As Range
    Dim xStr As String
    Dim xRegEx As RegExp
    Dim xMatch As Match
    Dim xMatches As MatchCollection
    Set xRegEx = New RegExp
    Application.EnableEvents = False
    Set xRg = Cells.SpecialCells(xlCellTypeConstants)
    With xRegEx
        .Global = True
        .Pattern = "<(""[^""]*""|'[^']*'|[^'"">])*>"
    End With
    For Each xCell In xRg
        xStr = xCell.Value
            Set xMatches = xRegEx.Execute(xCell.Text)
            For Each xMatch In xMatches
                xStr = Replace(xStr, xMatch.Value, "")
            Next
        xCell.Value = xStr
    Next
   Application.EnableEvents = True
End Sub

3. Vẫn trong cửa sổ Microsoft Visual Basic for Applications, vui lòng nhấp vào Tools > References, kiểm tra Microsoft VBScript Regular Expression 5. 5 trong hộp thoại References-VBAProject, rồi bấm nút OK

Tại sao phân tích cú pháp HTML trong Excel VBA? . Một số trường hợp đang tạo nhiều tệp HTML dựa trên dữ liệu excel, chỉnh sửa nhiều tệp HTML, loại bỏ một số dữ liệu, v.v.

Tôi đang sử dụng trang chủ Hacker News cho ví dụ này, nơi chúng tôi phân tích tất cả các bài đăng từ trang chủ. Tất nhiên, Hacker News có API riêng có thể được sử dụng để lấy các chủ đề mới nhất nhưng ví dụ này chỉ để tìm hiểu cách phân tích cú pháp HTML

Tại sao tin tức Hacker?

Đầu ra cuối cùng trông giống như hình ảnh dưới đây

Làm cách nào để chuyển đổi HTML sang Excel bằng mã VBA?

Bắt đầu

  1. Thư viện đối tượng HTML của Microsoft được sử dụng để phân tích cú pháp HTML
  2. Mở trình chỉnh sửa tập lệnh trong excel (alt + f11) và thêm tham chiếu vào thư viện đối tượng HTML của Microsoft (Công cụ > tham chiếu > chọn)

Một vài điều cơ bản trước và sau đó đi sâu vào mã

Định nghĩa và cài đặt HTML

Đối tượng HTML có thể được xác định bằng cách sử dụng

Dim html As New HTMLDocument

HTML có thể được đặt thành đối tượng này bằng cú pháp này

html.body.innerHTML = htmlstring

Các phương thức và thuộc tính hữu ích

Có nhiều phương thức và thuộc tính của đối tượng và phần tử HTML. Bạn có thể xem tất cả các phương thức sử dụng tính năng tự động hoàn thành nhưng các phương thức hữu ích nhất là các thuộc tính như sau

  1. getElementsByTagName
  2. getElementsByClassName
  3. getElementById
  4. getAttribute
  5. innerText
  6. innerHTML

bước

  1. Đầu tiên, chúng tôi kéo trang chủ Hacker News bằng cách thực hiện một yêu cầu HTTP GET cơ bản. Đọc thêm về các yêu cầu HTTP tại đây – Yêu cầu Http trong Excel VBA
  2. Đặt phản hồi HTTP cho đối tượng HTML của chúng tôi
  3. Nhận tất cả các chủ đề mới nhất bằng phương pháp
    html.body.innerHTML = htmlstring
    
    0
  4. Lặp lại từng chủ đề, phân tích tiêu đề, liên kết, lượt ủng hộ và tên người dùng của từng chủ đề bằng các phương pháp khác nhau
  5. Nhập tất cả các giá trị được phân tích cú pháp vào trang tính 1

Hoàn thành mã

Hãy nhìn vào mã đầu tiên

Public Sub parsehtml()
Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
Dim i As Integer
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", "https://news.ycombinator.com/", False
http.send
html.body.innerHTML = http.responseText
Set topics = html.getElementsByClassName("athing")
i = 2
For Each topic In topics
Set titleElem = topic.getElementsByTagName("td")(2)
Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
i = i + 1
Next
End Sub

Khi tôi nói chủ đề, ý tôi là bài viết

Giải thích mã

  1. Đầu tiên, chúng tôi xác định tất cả các đối tượng cần thiết như
    html.body.innerHTML = htmlstring
    
    1,
    html.body.innerHTML = htmlstring
    
    2
  2. Đối tượng chủ đề để lưu trữ các chủ đề sẽ được sử dụng để lặp. Tôi đã định nghĩa một vài đối tượng khác như
    html.body.innerHTML = htmlstring
    
    3 và
    html.body.innerHTML = htmlstring
    
    4 để làm cho mã có thể đọc được
  3. lấy trang chủ và đặt HTML của nó thành đối tượng HTML của chúng tôi ở dòng 7
  4. Bây giờ chúng ta phải lấy tất cả các yếu tố chủ đề. Chúng ta phải xác định những yếu tố đó để có được chúng. Trong trang html này, tất cả các phần tử chủ đề đều có một lớp tên là
    html.body.innerHTML = htmlstring
    
    5 nên chúng ta có thể sử dụng phương thức getElementsByClassName để lấy các phần tử đó
  5. Làm thế nào để xác định các yếu tố?
  6. Trong dòng 8, Chúng tôi lấy tất cả các chủ đề bằng cách sử dụng phương pháp getElementsByClassName bằng cách chỉ định tên lớp
    html.body.innerHTML = htmlstring
    
    5. Tiếp theo, chúng tôi lặp lại tất cả các chủ đề bằng cách sử dụng vòng lặp
    html.body.innerHTML = htmlstring
    
    9
  7. Tất cả các chủ đề nằm trong phần tử
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    0 và mỗi chủ đề là phần tử
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    1 (hàng) và thông tin chi tiết về chủ đề nằm trong phần tử
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    1 tiếp theo. Mỗi hàng có các phần phụ.
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    3 yếu tố có nội dung như tên chủ đề, tên miền, lượt ủng hộ, tên người dùng, v.v.

Làm cách nào để chuyển đổi HTML sang Excel bằng mã VBA?

  1. Tên chủ đề và tên miền nằm trong phần tử thứ ba
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    3 nên chúng tôi lấy nó bằng cách sử dụng
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    5 (Chỉ số bắt đầu từ 0). Tên chủ đề và liên kết nằm trong một phần tử
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    6 khác, vì vậy chúng tôi lấy nó bằng cách sử dụng
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    7 và nhập các giá trị của nó vào trang 1
  2. Các chi tiết về chủ đề như lượt ủng hộ và tên người dùng nằm trong phần tử tiếp theo của phần tử chủ đề, vì vậy chúng tôi lấy nó bằng phương pháp
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    8. Nhận upvote và tên người dùng trong các phần tử
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    9 và
    Public Sub parsehtml()
    Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
    Dim i As Integer
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://news.ycombinator.com/", False
    http.send
    html.body.innerHTML = http.responseText
    Set topics = html.getElementsByClassName("athing")
    i = 2
    For Each topic In topics
    Set titleElem = topic.getElementsByTagName("td")(2)
    Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
    Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href
    Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1)
    Sheets(1).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText
    Sheets(1).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText
    i = i + 1
    Next
    End Sub
    
    6 và nhập vào trang 1
  3. Số nguyên
    Public Function getXPathElement(sXPath As String, objElement As Object) As HTMLBaseElement
    Dim sXPathArray() As String
    
    Dim sNodeName As String
    Dim sNodeNameIndex As String
    Dim sRestOfXPath As String
    Dim lNodeIndex As Long
    Dim lCount As Long
    
    ' Split the xpath statement
    sXPathArray = Split(sXPath, "/")
    sNodeNameIndex = sXPathArray(1)
    If Not InStr(sNodeNameIndex, "[") > 0 Then
    sNodeName = sNodeNameIndex
    lNodeIndex = 1
    Else
    sXPathArray = Split(sNodeNameIndex, "[")
    sNodeName = sXPathArray(0)
    lNodeIndex = CLng(Left(sXPathArray(1), Len(sXPathArray(1)) - 1))
    End If
    sRestOfXPath = Right(sXPath, Len(sXPath) - (Len(sNodeNameIndex) + 1))
    
    Set getXPathElement = Nothing
    For lCount = 0 To objElement.ChildNodes().Length - 1
    If UCase(objElement.ChildNodes().item(lCount).nodeName) = UCase(sNodeName) Then
    If lNodeIndex = 1 Then
    If sRestOfXPath = "" Then
    Set getXPathElement = objElement.ChildNodes().item(lCount)
    Else
    Set getXPathElement = getXPathElement(sRestOfXPath, objElement.ChildNodes().item(lCount))
    End If
    End If
    lNodeIndex = lNodeIndex - 1
    End If
    Next lCount
    End Function
    
    1 được sử dụng để lưu trữ số hàng bắt đầu từ 2 và tăng dần theo từng chủ đề

kết thúc

Các phần tử HTML cũng có thể được định nghĩa là

Public Function getXPathElement(sXPath As String, objElement As Object) As HTMLBaseElement
Dim sXPathArray() As String

Dim sNodeName As String
Dim sNodeNameIndex As String
Dim sRestOfXPath As String
Dim lNodeIndex As Long
Dim lCount As Long

' Split the xpath statement
sXPathArray = Split(sXPath, "/")
sNodeNameIndex = sXPathArray(1)
If Not InStr(sNodeNameIndex, "[") > 0 Then
sNodeName = sNodeNameIndex
lNodeIndex = 1
Else
sXPathArray = Split(sNodeNameIndex, "[")
sNodeName = sXPathArray(0)
lNodeIndex = CLng(Left(sXPathArray(1), Len(sXPathArray(1)) - 1))
End If
sRestOfXPath = Right(sXPath, Len(sXPath) - (Len(sNodeNameIndex) + 1))

Set getXPathElement = Nothing
For lCount = 0 To objElement.ChildNodes().Length - 1
If UCase(objElement.ChildNodes().item(lCount).nodeName) = UCase(sNodeName) Then
If lNodeIndex = 1 Then
If sRestOfXPath = "" Then
Set getXPathElement = objElement.ChildNodes().item(lCount)
Else
Set getXPathElement = getXPathElement(sRestOfXPath, objElement.ChildNodes().item(lCount))
End If
End If
lNodeIndex = lNodeIndex - 1
End If
Next lCount
End Function
2 để tự động hoàn thành

Có nhiều cách để xác định một phần tử trong HTML. Sử dụng ID, Tên lớp, Tên thẻ, v.v. XPath cũng có thể được sử dụng để xác định phần tử nhưng VBA không có hỗ trợ tích hợp cho XPath. Đây là một chức năng tùy chỉnh để xác định các phần tử bằng XPath

Public Function getXPathElement(sXPath As String, objElement As Object) As HTMLBaseElement
Dim sXPathArray() As String

Dim sNodeName As String
Dim sNodeNameIndex As String
Dim sRestOfXPath As String
Dim lNodeIndex As Long
Dim lCount As Long

' Split the xpath statement
sXPathArray = Split(sXPath, "/")
sNodeNameIndex = sXPathArray(1)
If Not InStr(sNodeNameIndex, "[") > 0 Then
sNodeName = sNodeNameIndex
lNodeIndex = 1
Else
sXPathArray = Split(sNodeNameIndex, "[")
sNodeName = sXPathArray(0)
lNodeIndex = CLng(Left(sXPathArray(1), Len(sXPathArray(1)) - 1))
End If
sRestOfXPath = Right(sXPath, Len(sXPath) - (Len(sNodeNameIndex) + 1))

Set getXPathElement = Nothing
For lCount = 0 To objElement.ChildNodes().Length - 1
If UCase(objElement.ChildNodes().item(lCount).nodeName) = UCase(sNodeName) Then
If lNodeIndex = 1 Then
If sRestOfXPath = "" Then
Set getXPathElement = objElement.ChildNodes().item(lCount)
Else
Set getXPathElement = getXPathElement(sRestOfXPath, objElement.ChildNodes().item(lCount))
End If
End If
lNodeIndex = lNodeIndex - 1
End If
Next lCount
End Function

Nếu bạn có bất kỳ câu hỏi hoặc phản hồi nào, hãy bình luận bên dưới

Nhận thông báo khi có bài đăng mới bằng cách nhấp vào

Làm cách nào để chuyển đổi HTML sang Excel bằng mã VBA?
ở dưới cùng bên trái

Cần giúp đỡ?

Để trống trường này nếu bạn là người.

rajith kumar

Một CA- do giáo dục, lập trình viên tự học bằng niềm đam mê, thích khám phá các công nghệ mới và tin tưởng vào việc học bằng cách làm

Làm cách nào để trích xuất dữ liệu từ tệp HTML sang Excel bằng VBA?

Do đó, trước tiên thêm tham chiếu Thư viện đối tượng HTML của Microsoft vào ứng dụng . Từ menu trên cùng của trình soạn thảo VBA, nhấp vào Công cụ -> Tài liệu tham khảo…. Trong cửa sổ References tìm và chọn Microsoft HTML Object Library rồi nhấn OK. Ghi chú. Bạn có thể thấy đầu ra của ví dụ này trong cửa sổ ngay lập tức của trình soạn thảo VBA của bạn.

Chúng tôi có thể chuyển đổi HTML sang Excel không?

Để chuyển đổi định dạng HTML sang Excel, chỉ cần kéo và thả tệp HTML vào khu vực tải lên dữ liệu, chỉ định các tùy chọn chuyển đổi, nhấp vào nút 'Chuyển đổi' và nhận tệp Excel đầu ra của bạn sau vài giây

Bạn có thể sử dụng HTML trong VBA không?

HTML chỉ là một tệp văn bản thuần túy. bạn có thể viết ra bằng cách sử dụng các công cụ tệp tích hợp sẵn trong VBA hoặc sử dụng Scripting FileSystemObject .