2013-05-15 16 views
0

當我從一個網頁獲取數據到一個Excel文件使用宏它將數據保存在Excel文件,因爲它應該是。我的要求是,它應該將數據保存在Excel的超鏈接中,如果我們單擊Excel中的任何超鏈接,它應該到特定的網站並獲取數據並將信息保存在Excel文件或表格中。 我希望它是有道理的。任何幫助將不勝感激。從一個網站的數據提取到一個Excel文件中的超鏈接

我的宏下面的代碼:

Sub GetTable() 

Dim ieApp As InternetExplorer 
Dim ieDoc As Object 
Dim ieTable As Object 
Dim clip As DataObject 

'create a new instance of ie 
Set ieApp = New InternetExplorer 

'you don’t need this, but it’s good for debugging 
ieApp.Visible = True 

'assume we’re not logged in and just go directly to the login page 
ieApp.Navigate "website link" 
Do While ieApp.Busy: DoEvents: Loop 
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop 

Set ieDoc = ieApp.Document 

'fill in the login form – View Source from your browser to get the control names 
With ieDoc.forms(0) 
    .user.Value = "UserNmae 
    .Password.Value = "password" 
    .submit 
End With 
Do While ieApp.Busy: DoEvents: Loop 
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop 

'now that we’re in, go to the page we want 
ieApp.Navigate "final webpage link" 
Do While ieApp.Busy: DoEvents: Loop 
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop 

'get the table based on the table’s id 
Set ieDoc = ieApp.Document 
Set ieTable = ieDoc.all.Item("AutoNumber1") 

'copy the tables html to the clipboard and paste to teh sheet 
If Not ieTable Is Nothing Then 
    Set clip = New DataObject 
    clip.SetText "<html>" & ieTable.outerHTML & "</html>" 
    clip.PutInClipboard 
    Sheet1.Select 
    Sheet1.Range("A1").Select 
    Sheet1.PasteSpecial "Unicode Text" 
End If 

'close 'er up 
ieApp.Quit 
Set ieApp = Nothing 

End Sub 
+0

感謝您的編輯.. –

+0

您的目標的兩個直接問題。 Excel將只顯示單元格的前1,000個字符。 Excel超鏈接僅在單元級別運行。 –

+0

好吧,假設你在單元格中有超鏈接,單擊測試單元格內容以獲得URL /超鏈接,如果是這樣,請執行上面的類似方法。 –

回答

0

,你可以使用此代碼從Web瀏覽器中得到的所有超鏈接,我想你可以很容易地從列表框中將數據導出到Excel

Dim links As HtmlElementCollection = WebBrowser1.Document.Links 
     For Each link As HtmlElement In links 
      ListBox1.Items.Add(link.GetAttribute("href")) 
     Next 

Source

相關問題