对象需要vba中的错误 - 定义变量

时间:2017-10-17 15:58:08

标签: vba excel-vba excel

我是VBA的新手,需要帮助。有人可以请帮助。当我执行时,我需要对象。我没有使用Option Explicit。谢谢。

在下面的行中收到错误。

ie.document.getElementById("dp-status-dropdown_1").Click

已发布大部分代码。请帮忙。

Sub DataUpdate()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.DisplayAlerts = False

Dim ie As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim htmlinouts As MSHTML.IHTMLElementCollection
Dim out As MSHTML.IHTMLElement
Dim urr As Range
Dim Count As Long
Dim i As Long

Set ie = New InternetExplorer
'        htmldoc = ie.document

Count = ActiveSheet.Cells(Rows.Count, "H").End(xlUp).Row

For i = 4 To Count

    'For Each urr In Application.ThisWorkbook.Sheets("Dump").Range("H5:H" & Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("Dump").Range("H:H"))).Cells
    Set urr = Worksheets("Dump").Cells(i, 8)
    ie.Visible = True
    ie.navigate urr                          'H4 holds the URL
    Do
        DoEvents
    Loop Until ie.readyState = READYSTATE_COMPLETE

    ThisWorkbook.Sheets("Dump").Activate
    Dim cellvalue As Range
    Set cellvalue = ThisWorkbook.Sheets("Dump").Range("J4")
    If cellvalue = "" Then
        cellvalue = ""
    ElseIf cellvalue = "Pitch in Progress " Then 
        ie.document.getElementById("a-autoid-0-announce").Click
        ie.document.getElementById("dp-status-dropdown_0").Click
        Dim xx1 As Object
        For Each xx1 In ie.document.getElementsByClassName("a-button-text")
            If xx1.innerText Like "*OK*" Then
                ' Debug.Print xx1.innerText
                xx1.Click
                Exit For
            End If
        Next xx1
    ElseIf cellvalue = "Not Reachable" Then 
        ie.document.getElementById("a-autoid-0-announce").Click
        ie.document.getElementById("dp-status-dropdown_1").Click
        Dim xx2 As Object
        For Each xx2 In ie.document.getElementsByClassName("a-button-text")
            If xx2.innerText Like "*OK*" Then
                ' Debug.Print xx1.innerText
                xx2.Click
                Exit For
            End If
        Next xx2
    ElseIf cellvalue = "Not Interested" Then 
        ie.document.getElementById("a-autoid-0-announce").Click
        ie.document.getElementById("dp-status-dropdown_2").Click
        Dim xx3 As Object
        For Each xx3 In ie.document.getElementsByClassName("a-button-text")
            If xx3.innerText Like "*OK*" Then
                ' Debug.Print xx1.innerText
                xx3.Click
                Exit For
            End If
        Next xx3
    ElseIf cellvalue = "Work Started" Then  
        ie.document.getElementById("a-autoid-0-announce").Click
        ie.document.getElementById("dp-status-dropdown_3").Click
        Dim xx4 As Object
        For Each xx4 In ie.document.getElementsByClassName("a-button-text")
            If xx4.innerText Like "*OK*" Then
                ' Debug.Print xx1.innerText
                xx4.Click
                Exit For
            End If
        Next xx4
    ElseIf cellvalue = "Work Completed" Then 
        ie.document.getElementById("a-autoid-0-announce").Click
        ie.document.getElementById("dp-status-dropdown_4").Click
        Dim xx5 As Object
        For Each xx5 In ie.document.getElementsByClassName("a-button-text")
            If xx5.innerText Like "*OK*" Then
                ' Debug.Print xx1.innerText
                xx5.Click
                Exit For
            End If
        Next xx5
    ElseIf cellvalue = "Products picked up" Then 
        ie.document.getElementById("a-autoid-0-announce").Click
        ie.document.getElementById("dp-status-dropdown_4").Click
        Dim xx6 As Object
        For Each xx6 In ie.document.getElementsByClassName("a-button-text")
            If xx6.innerText Like "*OK*" Then
                ' Debug.Print xx1.innerText
                xx6.Click
                Exit For
            End If
        Next xx6
    ElseIf cellvalue = "Products in transit" Then 
        ie.document.getElementById("a-autoid-0-announce").Click
        ie.document.getElementById("dp-status-dropdown_5").Click
        Dim xx7 As Object
        For Each xx7 In ie.document.getElementsByClassName("a-button-text")
            If xx7.innerText Like "*OK*" Then
                ' Debug.Print xx1.innerText
                xx7.Click
                Exit For
            End If
        Next xx7
    ElseIf cellvalue = "Products delivered" Then 
        ie.document.getElementById("a-autoid-0-announce").Click
        ie.document.getElementById("dp-status-dropdown_6").Click
        Dim xx8 As Object
        For Each xx8 In ie.document.getElementsByClassName("a-button-text")
            If xx8.innerText Like "*OK*" Then
                ' Debug.Print xx1.innerText
                xx8.Click
                Exit For
            End If
        Next xx8
    ElseIf cellvalue = "MoU Signed" Then    
        ie.document.getElementById("a-autoid-0-announce").Click
        ie.document.getElementById("dp-status-dropdown_3").Click
        Dim xx9 As Object
        For Each xx9 In ie.document.getElementsByClassName("a-button-text")
            If xx9.innerText Like "*OK*" Then
                ' Debug.Print xx1.innerText
                xx9.Click
                Exit For
            End If
        Next xx9
    ElseIf cellvalue = "Subscription ended" Then 
        ie.document.getElementById("a-autoid-0-announce").Click
        ie.document.getElementById("dp-status-dropdown_4").Click
        Dim xx10 As Object
        For Each xx10 In ie.document.getElementsByClassName("a-button-text")
            If xx10.innerText Like "*OK*" Then
                ' Debug.Print xx1.innerText
                xx10.Click
                Exit For
            End If
        Next xx10

    End If
    Dim FollowupDate As Range           
    Set FollowupDate = ThisWorkbook.Sheets("Dump").Range("K4")
    If FollowupDate = "" Then
        FollowupDate = ""
    Else
        ie.document.getElementById("pc-followUpDate-edit").Click
        ie.document.getElementById("pc-followUpDate-input").Value = FollowupDate
        ie.document.getElementById("pc-followUpDate-button").Click
    End If

    Dim pprsrc As Range         
    Set pprsrc = ThisWorkbook.Sheets("Dump").Range("L4")
    If pprsrc = "" Then
        pprsrc = ""
    ElseIf pprsrc = "Call back scheduled" Then
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-edit").Click
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-input_1").Click
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-button").Click
    ElseIf pprsrc = "Price negotiation" Then
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-edit").Click
        ie.document.getElementById("a-autoid-30-announce").Click
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-input_2").Click
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-button").Click
    ElseIf pprsrc = "Seller revert awaited" Then
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-edit").Click
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-input_3").Click
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-button").Click
    ElseIf pprsrc = "products delayed" Then
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-edit").Click
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-input_4").Click
        ie.document.getElementById("pc-pitchInProgressRequestStatusReasonCode-button").Click
    End If
    Dim nirsrc As Range        
    Set nirsrc = ThisWorkbook.Sheets("Dump").Range("M4")
    If nirsrc = "" Then
        nirsrc = ""
    ElseIf nirsrc = "Pricing issues" Then
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-edit").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-input_1").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-button").Click
    ElseIf nirsrc = "Using another SP" Then
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-edit").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-input_2").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-button").Click
    ElseIf nirsrc = "In house capability" Then
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-edit").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-input_3").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-button").Click
    ElseIf nirsrc = "No current requirement" Then
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-edit").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-input_4").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-button").Click
    ElseIf nirsrc = "Non serviceable" Then
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-edit").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-input_5").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-button").Click
    ElseIf nirsrc = "Not Applicable" Then
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-edit").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-input_0").Click
        ie.document.getElementById("pc-notInterestedRequestStatusReasonCode-button").Click
    End If
    Dim SM As Range       
    Set SM = ThisWorkbook.Sheets("Dump").Range("N4")
    If SM = "" Then
        SM = ""
    Else
        ie.document.getElementById("pc-salesManagerName-edit").Click
        ie.document.getElementById("pc-salesManagerName-input").Value = SM
        ie.document.getElementById("pc-salesManagerName-button").Click
    End If
    Dim Comments As Range           
    Set Comments = ThisWorkbook.Sheets("Dump").Range("O4")
    If Comments = "" Then
        Comments = ""
    Else
        ie.document.getElementById("pc-providerComments-edit").Click
        ie.document.getElementById("pc-providerComments-input").Value = Comments
        ie.document.getElementById("pc-providerComments-button").Click
    End If  
    ie.document.getElementById("save-detail-btn-announce").Click 
Next i

End Sub

1 个答案:

答案 0 :(得分:0)

如果您尚未初始化正在使用的“ie”对象,则会出现对象所需错误。 此外,缩进代码应该看起来像这样。

Dim cellvalue As String
cellvalue = "Pitch in Progress "

If cellvalue = "" Then
    cellvalue = ""
ElseIf cellvalue = "Pitch in Progress " Then                                            '---------- Pitch in Progress --------
    ie.Document.getElementById("a-autoid-0-announce").Click
    ie.Document.getElementById("dp-status-dropdown_0").Click
    Dim xx1 As Object
    For Each xx1 In ie.Document.getElementsByClassName("a-button-text")
        If xx1.innerText Like "*OK*" Then
         ' Debug.Print xx1.innerText
            xx1.Click
            Exit For
        End If
    Next xx1
ElseIf cellvalue = "Not Reachable" Then                                       '--------- Not Reachable -----------
    ie.Document.getElementById("a-autoid-0-announce").Click
    ie.Document.getElementById("dp-status-dropdown_1").Click
    Dim xx2 As Object
    For Each xx2 In ie.Document.getElementsByClassName("a-button-text")
        If xx2.innerText Like "*OK*" Then
            ' Debug.Print xx1.innerText
            xx2.Click
            Exit For
        End If
    Next xx2
End If

请注意,我在最后添加了一个“End if”,并且我已经声明了一个名为cellvalue的变量并将其设置为“Pitch in Progress”,以便它在代码中运行,并且在此行上抛出错误:

ie.Document.getElementById("a-autoid-0-announce").Click