VBA将文本从IE复制到Excel
有人能帮我用一个简单的Visual Basic脚本将qwidget_lastsale字段从以下站点复制到excel工作表吗 我一直试图修改现有的脚本,但似乎无法使其正常工作。到目前为止,我可以打开网站和excel,但无法复制该字段 我希望在此之后复制脚本:VBA将文本从IE复制到Excel,excel,internet-explorer,vbscript,copy,Excel,Internet Explorer,Vbscript,Copy,有人能帮我用一个简单的Visual Basic脚本将qwidget_lastsale字段从以下站点复制到excel工作表吗 我一直试图修改现有的脚本,但似乎无法使其正常工作。到目前为止,我可以打开网站和excel,但无法复制该字段 我希望在此之后复制脚本: Set objExplorer = CreateObject("InternetExplorer.Application") WebSite = "http://www.nasdaq.com/symbol/abt/recommendatio
Set objExplorer = CreateObject("InternetExplorer.Application")
WebSite = "http://www.nasdaq.com/symbol/abt/recommendations"
with objExplorer
.Navigate2 WebSite
.left=5
.top=5
.height=1100
.width=700
.AddressBar = 0
.Visible = 1
.ToolBar = 0
.StatusBar = 1
WScript.Sleep 1000
Set objIE = Nothing
end with
Set xl = CreateObject("Excel.application")
xl.Application.Workbooks.Open "C:\Users\user\Documents\testauto.xlsx"
xl.Application.Visible = True
您需要使用DOM操作,并尽快从内存中释放
objExplorer
:
Set objExplorer = CreateObject("InternetExplorer.Application")
WebSite = "http://www.nasdaq.com/symbol/abt/recommendations"
Const READYSTATE_COMPLETE As Long = 4
With objExplorer
.Navigate2 WebSite
.Left=5
.Top=5
.Height=1100
.Width=700
.AddressBar = 0
.Visible = 1
.ToolBar = 0
.StatusBar = 1
'//WScript.Sleep 1000
Do Until .ReadyState = READYSTATE_COMPLETE
WScript.Sleep 1000
Loop
'//Set objIE = Nothing (your variable isn't called 'objIE' anyway?)
End With
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbooks.Open("C:\Users\user\Documents\testauto.xlsx")
Set ws = wb.Sheets("Sheet1") '// Change name of sheet as required
ws.Range("A1").Value = objExplorer.Document.getElementById("qwidget_lastsale").Value
'// Rest of code....
'// ...
'// NOW clear down your variables.
objExplorer.Quit
wb.Close SaveChanges:=True
xl.Quit
Set ws = Nothing
Set wb = Nothing
Set xl = Nothing
Set objExplorer = Nothing
另外,正如你在上面看到的,我改变了一些事情:
- Internet Explorer具有返回
的。您可以对此进行测试,以查看页面是否已加载,而不是等待1秒,并期待最好的结果Long
- 使用
创建Excel实例时,返回的对象就是应用程序对象-无需再次引用它。你会注意到我把那些拿了出来CreateObject(“Excel.Application”)
- 与Excel工作簿交互时,最好分别使用
、应用程序
和工作簿
对象的变量,并使用此变量。这样可以确保您始终在您想要的工作表上,并在正确的时间关闭正确的工作簿工作表
- 代码缩进是你的朋友-它使一切更容易阅读和遵循
Function Read(URL)
Set ie = Wscript.CreateObject("InternetExplorer.Application")
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
ie.Navigate(URL)
ie.Visible = 1
DO WHILE ie.busy
wscript.sleep 100
LOOP
Data = ie.document.documentElement.innertext
Msgbox(Data)
sp = Split(Data," ")
b = ubound(sp)
Msgbox(b)
For i=0 to b
Msgbox(sp(i))
Next
selectexcel=inputbox("Enter the location","Location of the excel file(Xls/xlsx)","Enter your path here !")
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(selectexcel)
objExcel.visible=True
rowCount=objExcel.ActiveWorkbook.Sheets(1).UsedRange.Rows.count
colCount=objExcel.ActiveWorkbook.Sheets(1).UsedRange.Columns.count
For i=1 to b Step 1
For j=1 to 1 Step 1
objExcel.Cells(i,j).Value=sp(i)
k=k+1
Next
Next
End Function
Read "http://www.nasdaq.com/symbol/abt/recommendations"
Set ie = Nothing
在这段代码中,我已将网站中的数据按“”字符拆分。谢谢你的提示和建议。我想继续宏Mans的建议,但出现错误:对象不支持此属性或方法'objExplorer.Document.getElementByID(…).Value'。代码800A01B6。我可能在页面上选择了错误的元素,或者为什么不支持它?我设法使用.innerText而不是.Value使它与上面的建议一起工作。谢谢你的帮助。