下午好,
首先,我知道使用ie做任何事情都不是很好,因为它不再被支持了。
我已经知道如何抓取一个表,但我需要将表数据放置在单元格A5中。我尝试将. range("A5")添加到部分代码中,但无法使其工作。请参见下面的代码:
Private Sub CommandButton3_Click()
'Clear the range before scraping
ActiveSheet.Range("A5:k5000").ClearContents
'Navigating to webpage
Dim ie As Object
Dim url As String
url = "https://www.myfueltanksolutions.com/validate.asp"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate url
Do While ie.Busy: DoEvents: Loop
Do Until ie.readyState = 4: DoEvents: Loop
'Login credentails and submit
Dim idoc As MSHTML.HTMLDocument
Set idoc = ie.document
idoc.all.CompanyID.Value = "CompanyID"
idoc.all.UserId.Value = "UserID"
idoc.all.Password.Value = "Password"
idoc.parentWindow.execScript "submitForm();"
Do While ie.Busy: DoEvents: Loop
Do Until ie.readyState = 4: DoEvents: Loop
'Scrapging table
Dim tbl As HTMLTable
Set tbl = ie.document.getElementById("RecentInventorylistform")
Dim rowcounter As Integer
Dim colcounter As Integer
rowcounter = 1
colcounter = 1
Dim tr As HTMLTableRow
Dim td As HTMLTableCell
Dim th
Dim mySh As Worksheet
Set mySh = ThisWorkbook.Sheets("Sheet1")
For Each tr In tbl.getElementsByTagname("tr")
'Loop thru table cells
For Each td In tr.getElementsByTagname("td")
mySh.Cells(rowcounter, colcounter).Value = td.innerText
colcounter = colcounter + 1
Next td
colcounter = 1
rowcounter = rowcounter + 1
Next tr
'Log out and close website
ie.navigate ("https://www.myfueltanksolutions.com/signout.asp?action=rememberlogin")
ie.Quit
'Last updated and message box at completion
Range("N1") = Now()
MsgBox "Data Imported Successfully. Press Ok to Continue."
End Sub
非常感谢你的帮助!
1条答案
按热度按时间eqqqjvef1#
是否从单元格A5开始?如果是,则应更改
mySh.Cells(rowcounter, colcounter).Value
中的值。单元格A5是Cells(5, 1)
,因此应从Cells(5, 1)
开始。您可以尝试按如下方式更改代码: