VBA提取无名称/ID表格数据并写入Excel的技术求助
Hey Pedro, let's get that Parte data into your Excel sheet! Your existing code does a solid job navigating the TRF1 site—we just need to add the pieces to extract the target info and write it to your workbook. Here's how to tweak your code:
First, make sure your ieBusy helper function is defined (I’m guessing you have this, but just in case):
Sub ieBusy(ie As InternetExplorer) Do While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE DoEvents Loop End Sub
Updated Full Code with Data Extraction & Excel Write
Sub GetTRF1PartesData() Dim ie As InternetExplorer Dim numero As String Dim HTMLDoc As HTMLDocument Dim elemUnique As Object Dim elemCollection As Object Dim partesInfo As String Dim objElement As Object numero = Application.InputBox("Por Favor entre com o numero precatorio", "", "") ' Initialize IE and navigate to the site Set ie = New InternetExplorer ie.Visible = True ie.Navigate "https://processual.trf1.jus.br/consultaProcessual/numeroProcesso.php?secao=TRF1&enviar=ok" ieBusy ie ' Fill in the process number and submit ie.Document.all("proc").innerText = numero ie.Document.getElementById("enviar").Click ieBusy ie ' Click the "Partes" link Set elemCollection = ie.Document.getElementsByTagName("a") For Each elemUnique In elemCollection If elemUnique.innerText Like "Partes" Then elemUnique.Click Exit For End If Next elemUnique ' Wait for the Partes page to fully load ieBusy ie ' Extract the Parte information (adjust selector based on actual page structure) On Error Resume Next ' Handle case where element isn't found ' Use a CSS selector to target the element with "requerente" text ' Inspect the page (F12 in IE) to find the exact selector if this doesn't work Set objElement = ie.Document.querySelector("td:contains('requerente')") If Not objElement Is Nothing Then partesInfo = objElement.ParentNode.innerText ' Grab the full row text Else partesInfo = "Não foi possível encontrar as informações das Partes" End If On Error GoTo 0 ' Write the data to Excel (example: cell A1 of the active sheet) ThisWorkbook.ActiveSheet.Range("A1").Value = partesInfo ' Cleanup resources ie.Quit Set ie = Nothing Set objElement = Nothing Set elemCollection = Nothing End Sub
Key Additions Explained:
- Post-"Partes" Load Wait: We call
ieBusyagain after clicking the "Partes" link to ensure the new page finishes loading before we try to extract data—this avoids errors from trying to access elements that aren’t there yet. - Data Extraction: The
querySelectorline targets the table cell containing "requerente". If this doesn’t work, right-click the Parte info on the page, select "Inspect Element" (in IE) to find the exact HTML tag/class/id, then adjust the selector. For example, if the info is in a<div>with classpartes-container, use".partes-container"instead. - Excel Write: The line
ThisWorkbook.ActiveSheet.Range("A1").Value = partesInfodumps the extracted text into cell A1. ChangeA1to any range you need (e.g.,Range("C5")or a dynamic cell likeCells(Rows.Count, 1).End(xlUp).Offset(1, 0)to add to the next empty row). - Cleanup: We quit IE and clear all objects to free up system resources—always a good practice with IE automation.
Quick Checks:
- Ensure you have the Microsoft Internet Controls and Microsoft HTML Object Library references enabled (go to Tools > References in the VBA editor to tick these boxes).
- If the CSS selector doesn’t work, try using
getElementsByClassNameorgetElementsByTagNameinstead—inspecting the page’s HTML will guide you to the right method.
内容的提问来源于stack exchange,提问作者pedro tedesco
相关产品推荐
相关产品推荐

