VBA code to get data from web

bhinangt

New Member
Joined
Mar 21, 2017
Messages
36
i want ONLY buy and sell price in excel from https://coins.co.th/....
Is there any VBA code that can help?

I tried Data>Import from Web menu but that gets full web site content which takes too long time to load...

Thanks in advance :)
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
First, set a reference (VBE > Tools > References) to the following libraries...

Code:
1) Microsoft XML, v6.0 (or whatever version you have)

2) Microsoft HTML Object Library

Then try...

Code:
[COLOR=darkblue]Sub[/COLOR] GetBitcoinBuyAndSellPrice()

    [COLOR=darkblue]Dim[/COLOR] XMLReq [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]New[/COLOR] MSXML2.XMLHTTP60
    [COLOR=darkblue]Dim[/COLOR] HTMLDoc [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]New[/COLOR] MSHTML.HTMLDocument
    [COLOR=darkblue]Dim[/COLOR] sBuySell [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    XMLReq.Open "GET", "https://coins.co.th/", [COLOR=darkblue]False[/COLOR]
    XMLReq.send
    
    [COLOR=darkblue]If[/COLOR] XMLReq.Status <> 200 [COLOR=darkblue]Then[/COLOR]
        MsgBox "Error " & XMLReq.Status & ":  " & XMLReq.statusText
        [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    HTMLDoc.body.innerHTML = XMLReq.responseText
    
    sBuySell = HTMLDoc.getElementById("quote_string").innerText
    
    MsgBox sBuySell, vbInformation
    
    [COLOR=darkblue]Set[/COLOR] XMLReq = [COLOR=darkblue]Nothing[/COLOR]
    [COLOR=darkblue]Set[/COLOR] HTMLDoc = [COLOR=darkblue]Nothing[/COLOR]

[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

To split the buy and sell price, you can add the following lines of code...

Code:
    [COLOR=darkblue]Dim[/COLOR] sBuy [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sSell [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    sBuy = Trim(Split(sBuySell, "|")(0))
    sSell = Trim(Split(sBuySell, "|")(1))

Hope this helps!
 
Upvote 0
Thanks genius

First, set a reference (VBE > Tools > References) to the following libraries...

Code:
1) Microsoft XML, v6.0 (or whatever version you have)

2) Microsoft HTML Object Library

Then try...

Code:
[COLOR=darkblue]Sub[/COLOR] GetBitcoinBuyAndSellPrice()

    [COLOR=darkblue]Dim[/COLOR] XMLReq [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]New[/COLOR] MSXML2.XMLHTTP60
    [COLOR=darkblue]Dim[/COLOR] HTMLDoc [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]New[/COLOR] MSHTML.HTMLDocument
    [COLOR=darkblue]Dim[/COLOR] sBuySell [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    XMLReq.Open "GET", "https://coins.co.th/", [COLOR=darkblue]False[/COLOR]
    XMLReq.send
    
    [COLOR=darkblue]If[/COLOR] XMLReq.Status <> 200 [COLOR=darkblue]Then[/COLOR]
        MsgBox "Error " & XMLReq.Status & ":  " & XMLReq.statusText
        [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    HTMLDoc.body.innerHTML = XMLReq.responseText
    
    sBuySell = HTMLDoc.getElementById("quote_string").innerText
    
    MsgBox sBuySell, vbInformation
    
    [COLOR=darkblue]Set[/COLOR] XMLReq = [COLOR=darkblue]Nothing[/COLOR]
    [COLOR=darkblue]Set[/COLOR] HTMLDoc = [COLOR=darkblue]Nothing[/COLOR]

[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

To split the buy and sell price, you can add the following lines of code...

Code:
    [COLOR=darkblue]Dim[/COLOR] sBuy [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sSell [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    sBuy = Trim(Split(sBuySell, "|")(0))
    sSell = Trim(Split(sBuySell, "|")(1))

Hope this helps!
 
Upvote 0
You're very welcome!

Cheers!

Just need little extra input...

Because the price keep on changing whenever i run macro again, it must fetch new prices.. But in this case it shows the price of first time.. May be i need macro to clear old values and fetch new values....


Also can you suggest how this changing values can be recorded sequentially with time stamp every 1 minute??
 
Upvote 0
To record, you need real database - not Excel.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,809
Members
452,944
Latest member
2558216095

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