背景
我需要抓取数据,因为我无法直接访问源数据.这是我公司内部批准的活动.
我不被允许发布html的任何部分;但是,因为我已经验证了代码的抓取部分,所以不应该这样做.
我写了一个VBA宏:
>打开Internet Explorer窗口
>导航到Intranet网站
>在一个工作表中加载一个提交单元格内容的网站
>进入客户记录的流程
>查找特定的网站对象ID
>将特定ID中的值保存到同一工作簿中的第二个工作表
>释放记忆
>退出
问题:
>数据不会保存到工作表中.
我试过的:
>创建新的输出工作表
>使用现有输出工作表
>使用工作簿上的“保存”,“激活”和“选择”命令
>通过以下方式引用工作表:
>代码名称
>工作表名称
>索引
>详尽的试验和错误以及相当数量的研究
代码:
Option Explicit
Sub GetxyzData()
Dim rowCount As Integer
Dim colCount As Integer
Dim objIE As InternetExplorer
Dim ele As Object
Dim startRange As Range
Dim NoteFound As Boolean
Dim ContactFound As Boolean
Dim itm As Object
'Create the IE Object
Set objIE = CreateObject("InternetExplorer.Application")
'Set the position and size attributes of the IE Object
objIE.Top = 0
objIE.Left = 0
objIE.Width = 800
objIE.Height = 600
'Set the visibility of the IE Object
objIE.Visible = True
'Check to see if there was an error with the website
On Error Resume Next
objIE.navigate ("http://xyz/xyz_Individual/Applications/xyz/SearchMain.aspx/")
'Wait until the website is ready to begin along with error checking
Do While objIE.Busy
DoEvents
'Check to see if there was an error loading the website
If Err.Number <> 0 Then
objIE.Quit
Set objIE = Nothing
GoTo Program_Exit
End If
'Wait until the website is ready to begin
Application.StatusBar = "Connecting to Website..."
DoEvents
Loop
'Set the Row Number to 1 since there is a header row
rowCount = 1
'Set the data entry into Excel in the First Column and row
startRange = "A1"
'Continue to loop through the Excel data until a blank entry is found in the ID Number column
Do While Sheet5.Range("K" & rowCount) <> ""
'Populate the Prospect ID Number in the search box with value in cell "K + Counter"
objIE.document.getElementById("ctl00$txtProspectid").innerText = _
"0" & Sheet5.Range("K" & rowCount).Value
'Click the search button
objIE.document.getElementById("ctl00_btnsearch").Click
'Wait until the website is ready to begin along with error checking
Do While objIE.Busy
Application.StatusBar = "Downloading information, Please wait..."
DoEvents
Loop
'Check to see if this is the first customer and click the appropriate Prospect hyperlink
If rowCount = 1 Then
objIE.document.getElementById("ctl00_GrdExtract_ctl03_btnsel").Click
Else
objIE.document.getElementById("ctl00_GrdMember_ctl03_lnkProspectID").Click
End If
'Wait until the website is ready to begin
Do While objIE.Busy
Application.StatusBar = "Downloading information, Please wait..."
DoEvents
Loop
'Set table type indicators to know when we are processing the 1st data field in each
NoteFound = False
ContactFound = False
'Get the data fields for PII, Contacts and Notes based on the common portion of ID name
With Sheets("MWData")
For Each itm In objIE.document.all
'If it is not a PII, Contact or Note field, then skip it
If itm.ID Like "*ctl00_CPH1_tabcontbottom_tabpnlContact_grdContact*" Or _
itm.ID Like "*ctl00_CPH1_tabcontbottom_tabpnlNotes_GrdUserNotes*" Or _
itm.ID Like "*ctl00_CPH1_tabconttop_TabpnlPI_txt*" Then
'Write itm.Value to screen if it is not blank
If itm.Value <> "" Then
MsgBox itm.Value
End If
' Check to see if it is the first Contact field for the customer, if so save the
' column number the last contact field holds and then increment the rowCounter to store
' the first field of the Note fields.
If itm.ID Like "*ctl00_CPH1_tabcontbottom_tabpnlContact_grdContact*" Then
'If this is the first Contact field then we want to save the the current colCount
If ContactFound = False Then
.Range(colCount & rowCount) = "ContactStart = " & colCount
colCount = rowCount + 1
ContactFound = True
End If
End If
' Check to see if it is the first Note field for the customer, if so save the
' column number the last note field holds
If itm.ID Like "*ctl00_CPH1_tabcontbottom_tabpnlNotes_GrdUserNotes*" Then
'If this is the first Note field then we want to save the the current colCount
If NoteFound = False Then
.Range(colCount & rowCount) = "NoteStart = " & colCount
colCount = rowCount + 1
NoteFound = True
End If
End If
' Store the fields value in the next available column on the same row
Sheets("MWData").Range(colCount & rowCount) = itm.Value
'Increment the column counter to the next to prepare to write the next field
colCount = colCount + 1
End If
Next itm
End With
'Increment the row counter and set the column counter back to 1
rowCount = rowCount + 1
colCount = 1
'Loop back to get the next customer entry
Loop
Application.StatusBar = "Download Complete....."
'Exit the program if there was an error retrieving the website
Program_Exit:
'Clean up system resources before ending the program
objIE.Quit
Set objIE = Nothing
End Sub
最佳答案 您在代码中多次使用此.Range(colCount& rowCount):
.Range(colCount& rowCount)=“ContactStart =”& colCount
.Range(colCount& rowCount)=“NoteStart =”& colCount
表格(“MWData”).范围(colCount& rowCount)= itm.Value
但是colCount和rowCount是整数,所以这不会起作用,例如你将得到Range(12),其中colCount = 1,rowCount = 2.
您可以像这样使用工作表的Cells集合,但不能使用Range对象,例如
表格(“MWData”).单元格(rowCount,colCount)= itm.Value