[VBA] Compile Error question

borski88

Board Regular
Joined
Jul 3, 2015
Messages
71
I am new to VBA and I am having an issue with this code, if someone could debug it for me that would be very much appreciated.
I edited out part of the URL for security reasons.

When I run this I get the following messege:

Compile Error:
User-defined type not defined.

and it highlights the following section:

Code:
       Sub WebQuery()
         
        'Add: (menu) Tools/References/'Microsoft XML V6.0' & 'Microsoft Forms 2.0 Object Library'
         
        Dim [COLOR=#ff0000][B]httpRequest As XMLHTTP[/B][/COLOR]      'XML V6.0
        Dim DataObj As New MSForms.DataObject   'Forms 2.0
         
        'clear any old data
        Sheets("QuerySheet").Activate
            For Each QT In ActiveSheet.QueryTables
                QT.Delete
            Next QT
        ActiveSheet.Cells.Clear
        
        Date = Format(Range("Main!B1"), "YYYY-MM-DD")
     
        'prepare data download
        Set httpRequest = New XMLHTTP
           httpRequest.Open "GET", "https://****.****.com/?end_date=" & Date & "&fc=****&start_date=" & Date & "&submit=Fetch+Data", False
           
        httpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        httpRequest.send ""
        'download data, and put in clipboard
        DataObj.SetText httpRequest.responseText
        DataObj.PutInClipboard
         
        'paste clipboard to sheet
        Sheets("QuerySheet").Range("C1").Select
        Sheets("QuerySheet").PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
                    
        Sheets("Main").Select
        Range("A1").Select
                    
        End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
found this as a function
Code:
<code>Function GetHTTPResult(sURL As String) As String
    Dim XMLHTTP As Variant, sResult As String

    Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    XMLHTTP.Open "GET", sURL, False
    XMLHTTP.Send
    Debug.Print "Status: " & XMLHTTP.Status & " - " & XMLHTTP.StatusText
    sResult = XMLHTTP.ResponseText
    Debug.Print "Length of response: " & Len(sResult)
    Set XMLHTTP = Nothing
    GetHTTPResult = sResult
End Function</code>

it might give clues how to format your needs

source excel vba http request download data from yahoo finance - Stack Overflow
 
Last edited:
Upvote 0
I am new to VBA and I am having an issue with this code, if someone could debug it for me that would be very much appreciated.
I edited out part of the URL for security reasons.

When I run this I get the following messege:

Compile Error:
User-defined type not defined.

and it highlights the following section:

Code:
       Sub WebQuery()
         
        [COLOR="#FF0000"][B]'Add: (menu) Tools/References/'Microsoft XML V6.0' & 'Microsoft Forms 2.0 Object Library'[/B][/COLOR]
         
        Dim [B]httpRequest As XMLHTTP[/B]      'XML V6.0
        Dim DataObj As New MSForms.DataObject   'Forms 2.0
         
        'clear any old data
        Sheets("QuerySheet").Activate
            For Each QT In ActiveSheet.QueryTables
                QT.Delete
            Next QT
        ActiveSheet.Cells.Clear
        
        Date = Format(Range("Main!B1"), "YYYY-MM-DD")
     
        'prepare data download
        Set httpRequest = New XMLHTTP
           httpRequest.Open "GET", "https://****.****.com/?end_date=" & Date & "&fc=****&start_date=" & Date & "&submit=Fetch+Data", False
           
        httpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        httpRequest.send ""
        'download data, and put in clipboard
        DataObj.SetText httpRequest.responseText
        DataObj.PutInClipboard
         
        'paste clipboard to sheet
        Sheets("QuerySheet").Range("C1").Select
        Sheets("QuerySheet").PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
                    
        Sheets("Main").Select
        Range("A1").Select
                    
        End Sub

You are getting that error because you did not set the Reference that what I commented in red instructed you to do.
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,351
Members
451,697
Latest member
pedroDH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top