2016-04-29 52 views
0

我是VBA的新手,如果此問題顯示格式不正確,請致歉,因爲我不知道如何在此處做得更好。無法使用VBA從班級名稱中刪除表格

目前,我試圖從網站備案,但沒有成功下載數據,它自帶了一個「運行時錯誤438,我也嘗試使用不同的metheods像

getElementsByClassName方法看爲「M_box」「M_content」「pub_table」
getElementsByID尋找「datatb」

但沒有成功。我也試過以下,但也未能

get.ElementByClassName( 「M_content」)(0).getElementsByID( 「datatb」)(1) get.ElementByClassName( 「M_content」)(0)。的getElementsByTagName( 「表」)(1)


Sub GetAsianOdds() 

Dim IE As Object 
Dim r As Integer, c As Integer, t As Integer, x As Integer 
Dim ElementHtml As Object 

Set IE = CreateObject("InternetExplorer.Application") 

With IE 

.Visible = True 
.navigate ("http://odds.500.com/fenxi/yazhi-567405") 

While IE.readyState <> 4 
DoEvents 
Wend 

MsgBox "IE is ready" 

Set ElementHtml = IE.Document.getElementsByClassName("pub_table") <--Run Time Error happens here 

For t = 0 To (ElementHtml.Length - 1) 
For r = 0 To (ElementHtml(t).Rows.Length - 1) 
For c = 0 To (ElementHtml(t).Rows(r).Cells.Length - 1) 
Set ThisWorkbook.Worksheets("Test").Cells(r + 1, c + 1).Value = ElementHtml(t).Rows(r).Cells(c).innerText 
Next c 
Next r 
Next t 

End With 

IE.Quit 
Set IE = Nothing 

End Sub 

並且在下面有關網站的部分的HTML代碼


<div class="mar_b yz_contrast"> 
<div class="M_box"> 
<div class="M_title"><h2>...</h2></div> 
<div class="M_content"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="pub_table" id="datatb"> 
<tr> 
<th class="th_one"><label>...</label></th> 
<th>...</th> 
<th width="55">...</th> 
<th width="120">...</th> 
<th width="55">...</th> 
<th>...</th> 
<th width="55">...</th> 
<th width="90">...</th> 
<th width="55">...</th> 
<th>...</th> 
<th>...</th> 
</tr> 

任何人都可以引導我如何解決這個問題呢?

非常感謝!

回答

0

這不是一個真正的答案,但我基本上與vba html類相同,它的工作原理。所以你可能想嘗試一下:

'make sure you add references to Microsoft Internet Controls (shdocvw.dll) and 
'Microsoft HTML object Library. 
'Code will NOT run otherwise. 

Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll) 
Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library 
Dim htmlInput As MSHTML.HTMLInputElement 
Dim htmlColl As MSHTML.IHTMLElementCollection 




Set objIE = New SHDocVw.InternetExplorer 


With objIE 
    .Navigate "http://worldoftanks.com/en/tournaments/1000000017/" ' Main page 
    .Visible = 0 
    Do While .READYSTATE <> 4: DoEvents: Loop 
     Application.Wait (Now + TimeValue("0:00:01")) 


     Set htmlDoc = .document 

     Dim ButtonRoundData As Variant 
     Set ButtonRoundData = htmlDoc.getElementsByClassName("group-stage_link") 

我希望這可以幫助,即使它不是一個答案,爲什麼你的代碼不能正常工作。