Excel在运行宏时关闭

  • 本文关键字:运行 Excel excel vba
  • 更新时间 :
  • 英文 :


这个VBA代码可以运行大约5-6个循环。之后,Excel文件关闭。有时,Excel文件在关机后会恢复。有时它不会。请帮我解决这个问题。我不是VBA专家来解决这个问题。谢谢。

Sub GetSpecificLinks()
'The code searches a website for the contact page url & pastes it in cell B2. 
'First define all the variables
Dim ie As Object 'Internet Explorer
Dim html As Object ' HTML document
Dim myLinks As Object ' Links collection
Dim myLink As Object 'Single Link
Dim result As String
Dim myURL As String 'Web Links on worksheet
Dim LastRow As Integer ' VBA execution should stop here
Set ie = CreateObject("InternetExplorer.Application")
LastRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
'Loop through all the web links on the worksheet one by one and then do some things
For i = 2 To LastRow
'Get the link from the worksheet and assign it to the variable
myURL = Sheet1.Cells(i, 1).Value
'Now go to the website
ie.navigate myURL
'Keep the internet explorer visible
ie.Visible = True
'Ensure that the web page has downloaded completely
While ie.readyState <> 4
DoEvents
Wend
'Get the data from the web page that is in the links and assign it to the variable
result = ie.document.body.innerHTML
'create a new html file
Set html = CreateObject("htmlfile")
'now place all the data extracted from the web page into the new html document
html.body.innerHTML = result
Set myLinks = html.getElementsByTagName("a")
'loop through the collected links and get a specific link defined by the conditions
For Each myLink In myLinks
If myLink Like "*contact*" Or myLink Like "*nquiry*" Or myLink Like "*investor*" Or myLink Like "*relation*" Then
Sheet1.Cells(i, "B").Value = myLink
End If
'go to the next link
Next myLink
'once the last web link on the sheet has been visited close the internet explorer
If i = LastRow Then
ie.Quit
End If
'go to the next web link on the worksheet
Next i
End Sub

这可能不是问题,但是您不需要检查最后一行来执行ie.quit。删除if语句并移动ie。

相关内容

  • 没有找到相关文章

最新更新