gpt4 book ai didi

vba - 将数据保存到工作表中

转载 作者:行者123 更新时间:2023-12-02 11:35:38 26 4
gpt4 key购买 nike

背景

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

我不允许发布 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

Sheets("MWData").Range(colCount & rowCount) = itm.Value

但是 colCountrowCount 是整数,因此这不起作用,例如您将得到 Range(12),其中 colCount = 1rowCount = 2

您可以像这样使用 WorksheetCells 集合,但不能使用 Range 对象,例如

Sheets("MWData").Cells(rowCount, colCount) = itm.Value

关于vba - 将数据保存到工作表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41904760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com