Khan Taimoor Malik
Khan Taimoor Malik

Reputation: 74

Getting this error when I run my VBA macro

Previous lines of codes are running successfully when compiler reached here this first line it through an exception.!

"91 Object Variable or with block variable not set"

I want to get HTML table data. Desired page is opened but cannot get table data. What wrong with this?

Sub tst()

'MsgBox
'Return

Dim username, password As String
Dim pages, rows, cols As Integer
Dim isbrowser As Boolean
Dim w, CODE, DESC, DOWNLOADS, x, url, temp, col, credentials
Dim html As Object

Sheets("Control").Select
credentials = Split(Sheets("Control").Cells(2, 2).Value, ":")
pages = Sheets("Control").Cells(2, 3).Value
isbrowser = Sheets("Control").Cells(2, 4).Value

username = credentials(0)
password = credentials(1)
rows = 2
cols = 6

' MsgBox username & "-" & password & "-" & pages & "-" & isbrowser
Sheets("Control").Cells(rows, cols).Value = "Downloading"
Sheets("Main").Cells.ClearContents
Const BetJBN As String = "login page url"

temp = col = w = CODE = DESC = DOWNLOADS = x = url = ""

On Error GoTo Err_Login

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = isbrowser

With IE
    .navigate BetJBN 'address of log in page
    Do While .busy: DoEvents: Loop
    Do While .readyState <> 4: DoEvents: Loop
    'this information is specific to the web page
    With .document.forms(0)
        .opername.Value = username
        .password.Value = password
        ''MsgBox "login"
        .submit
    End With
    MsgBox "Fetching start..."
    If pages = 0 Then
        url = "html table url"
        With IE
            .navigate url
            Do While .busy: DoEvents: Loop
            Do While .readyState <> 4: DoEvents: Loop
            With .document.forms(0)
                .Page.Value = username
                .searchmodel.Value = "buytimes"
                .op.Value = "search"
                ''MsgBox "Submit"
                .submit
            End With

            Set html = IE.document.getElementsByTagName("TABLE")(7)
            pages = Split(html.getElementsByTagName("TD")(0).innerText, "/")
            pages = Split(pages(1), " ")
            pages = pages(0)
            'MsgBox pages
            Sheets("Control").Select
            Sheets("Control").Cells(2, 3).Value = pages
            Set html = Nothing
            Sheets("Main").Cells.ClearContents
        End With
    End If
    'MsgBox pages

    x = 1
    For i = 1 To pages

        url = "html table url" & i

        'MsgBox url
        'MsgBox pages
        'Cells(x, 1).Value = "Page " & i
        'x = x + 1

        With IE
            .navigate url

            Do While .busy: DoEvents: Loop
            Do While .readyState <> 4: DoEvents: Loop

            Dim y As Integer
            y = -1

            Do Until y > IE.document.getElementsByTagName("TR").Length
                y = y + 1
                If IE.document.getElementsByTagName("TR")(y).className = "sptr2" Then

                    CODE = DESC = DOWNLOADS = ""
                    Set html = IE.document.getElementsByTagName("TR")(y)
                    CODE = html.getElementsByTagName("TD")(0).innerText
                    'DESC = html.getElementsByTagName("TD")(1).innerText
                    DESC = Format(Now, "DD-MMM-YY")
                    DOWNLOADS = html.getElementsByTagName("TD")(2).innerText
                    If CODE <> False Then
                        Sheets("Main").Cells(x, 1).Value = RBTCODE
                        Sheets("Main").Cells(x, 2).Value = DESC
                        Sheets("Main").Cells(x, 3).Value = DOWNLOADS
                        'MsgBox DOWNLOADS
                    Else
                        x = x - 1
                    End If
                    x = x + 1
                    Set html = Nothing
                End If
                On Error Resume Next
            Loop
        End With

    Next i

    MsgBox "Done with Fetching"
    Make_CSV (username)
    Sheets("Control").Select
    Sheets("Control").Cells(rows, cols).Value = "Done"
    IE.Quit
    Set IE = Nothing
    Sheets("Main").Cells.ClearContents
End With

Exit_Login:
Exit Sub

Err_Login:
MsgBox Err.Number & " " & Err.Description, vbOKOnly + vbCritical, "Error"
Resume Exit_Login

End Sub


Sub Make_CSV(user As String)
'
' CSV Macro
'
Dim sFile As String
Dim sBase As String
Dim sPath As String
Dim sDir As String

    sPath = "D:\"
    sBase = user & "_" & Format(Now, "YYYYMMDD")
    sFile = sBase & ".CSV"

    Sheets("Main").Select
    Sheets("Main").Copy Before:=Sheets(1)
    Sheets("Main (2)").Select
    Sheets("Main (2)").Name = sBase
    Range("B10").Select
    Sheets(sBase).Select
    sDir = "D:\"

    Sheets(sBase).Move
            ActiveWorkbook.SaveAs Filename:=sDir & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, _
                                  CreateBackup:=False
            ActiveWindow.Close

    Columns("A:C").Select
    Selection.Delete Shift:=xlToLeft
    Sheets("Control").Select

End Sub

Upvotes: 1

Views: 411

Answers (1)

bonCodigo
bonCodigo

Reputation: 14361

This isn't an answer. Really pointing out those that doesn't seem to be right...

Usually the error comes when you have declared object types and not using set when assigning a object reference type to them. Also please do a debug compile of your code before running it. Check your VBA editor settings and to break on all unhandled errors.

Your current declarations:

Dim username, password As String
Dim pages, rows, cols As Integer
Dim isbrowser As Boolean
Dim w, RBTCODE, DESC, DOWNLOADS, x, url, temp, col, credentials
Dim html As Object

username, pages, rows, w, RBTCODE, DESC, DOWNLOADS, x, url, temp, col, credentials are defined as variants in your code. You should provide proper data types separately for each of it.

row is declared as a variant by default. And I wonder how you managed to use it in this line and pass through.

Sheets("Control").Cells(rows, cols).Value = "Downloading"

Pages is declared as a variant by default.

pages = Sheets("Control").Cells(2, 3).Value
If pages = 0 Then

Upvotes: 1

Related Questions