Vba 将数据保存到工作表中

Vba 将数据保存到工作表中,vba,excel,Vba,Excel,背景 我需要刮取数据,因为我无法直接访问源数据。这是我公司内部批准的活动 我不允许发布html的任何部分;但是,由于我已经验证了代码的删除部分,所以不需要这样做 我编写了一个VBA宏: Option Explicit Sub GetxyzData() Dim rowCount As Integer Dim colCount As Integer Dim objIE As InternetExplorer Dim ele As Object Dim startRange As Range Di

背景

我需要刮取数据,因为我无法直接访问源数据。这是我公司内部批准的活动

我不允许发布html的任何部分;但是,由于我已经验证了代码的删除部分,所以不需要这样做

我编写了一个VBA宏:

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
  • 打开Internet Explorer窗口
  • 导航到内部网网站
  • 加载包含一个工作表中单元格内容的网站
  • 获取客户记录的过程
  • 查找特定的网站对象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
  • 数据将不会保存到工作表中
我所尝试的:

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
  • 创建新的输出工作表
  • 使用现有的输出工作表
  • 在工作簿上使用“保存”、“激活”和“选择”命令
  • 通过以下方式引用工作表:

    • 代号
    • 图纸名称
    • 索引
  • 详尽的试验和错误以及等量的研究

代码:

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
选项显式
子GetxyzData()
将行计数设置为整数
Dim colCount为整数
Dim objIE作为InternetExplorer
作为对象的Dim ele
暗星际范围
未找到布尔值
未找到布尔值
将itm调暗为对象
'创建IE对象
Set objIE=CreateObject(“InternetExplorer.Application”)
'设置IE对象的位置和大小属性
objIE.Top=0
左=0
对象宽度=800
物体高度=600
'设置IE对象的可见性
objIE.Visible=True
'检查网站是否有错误
出错时继续下一步
objIE.navigate(“http://xyz/xyz_Individual/Applications/xyz/SearchMain.aspx/")
'等待网站准备好开始错误检查
忙的时候去做
多芬特
'检查加载网站时是否出错
如果错误号为0,则
objIE,退出
设置对象=无
转到程序退出
如果结束
'等待网站准备好开始
Application.StatusBar=“连接到网站…”
多芬特
环
'将行号设置为1,因为存在标题行
行数=1
'在第一列和第一行将数据输入Excel
startRange=“A1”
'继续在Excel数据中循环,直到在ID编号列中找到一个空白条目
在5.范围(“K”和行计数)时执行此操作
'用单元格“K+计数器”中的值填充搜索框中的潜在客户ID号'
objIE.document.getElementById(“ctl00$txtProspectid”)。innerText=_
“0”和表5.范围(“K”和行计数).值
'单击搜索按钮
objIE.document.getElementById(“ctl00\u BTN搜索”)。单击
'等待网站准备好开始错误检查
忙的时候去做
Application.StatusBar=“正在下载信息,请稍候…”
多芬特
环
'检查这是否是第一位客户,然后单击相应的潜在客户超链接
如果rowCount=1,则
objIE.document.getElementById(“ctl00\u GrdExtract\u ctl03\u btnsel”)。单击
其他的
objIE.document.getElementById(“ctl00\u GrdMember\u ctl03\u lnkProspectID”)。单击
如果结束
'等待网站准备好开始
忙的时候去做
Application.StatusBar=“正在下载信息,请稍候…”
多芬特
环
'设置表类型指示器,以了解我们何时处理每个表中的第一个数据字段
NoteFound=False
ContactFound=False
'根据ID名称的公共部分获取PII、联系人和备注的数据字段
带图纸(“MWData”)
对于objIE.document.all中的每个itm
'如果不是PII、联系人或备注字段,则跳过它
如果itm.ID像“*ctl00\u CPH1\u tabcontbottom\u tabpnlContact\u grdContact*”或_
itm.ID如“*ctl00\u CPH1\u tabcontbottom\u tabpnlNotes\u GrdUserNotes*”或_
itm.ID类似于“*ctl00\u CPH1\u tabconttop\u TabpnlPI\u txt*”然后
'如果itm.值不为空,则将其写入屏幕
如果itm.Value为“”,则
MsgBox itm.值
如果结束
'检查是否为客户的第一个联系人字段,如果是,则保存
'最后一个联系人字段保留的列号,然后增加要存储的行计数器
'注释字段的第一个字段。
如果itm.ID像“*ctl00\u CPH1\u tabcontbottom\u tabpnlContact\u grdContact*”那么
'如果这是第一个联系人字段,则我们希望保存当前colCount
如果ContactFound=False,则
.Range(colCount和rowCount)=“ContactStart=”和colCount
colCount=rowCount+1
ContactFound=True
如果结束
如果结束
'检查它是否是客户的第一个备注字段,如果是,则保存
'最后一个注释字段包含的列编号
如果itm.ID像“*ctl00\u CPH1\u tabcontbottom\u tabpnlNotes\u GrdUserNotes*”那么
'如果这是第一个注释字段,则我们希望保存当前colCount
如果NoteFound=False,则
.Range(colCount和rowCount)=“NoteStart=”和colCount
colCount=rowCount+1
NoteFound=True
如果结束
如果结束
'将字段值存储在同一行的下一个可用列中
图纸(“MWData”).范围(colCount和rowCount)=itm.值
'将列计数器增加到下一个,以准备写入下一个字段
colCount=colCount+1
如果结束
下一个itm
以
'增加行计数器并将列计数器设置回1
rowCount=rowCount+1
colCount=1
'循环返回以获取下一个客户条目
环
Application.StatusBar=