Real Time Data in Excel - EBOOK VBA EXCEL

EBOOK VBA EXCEL

EVBA.info ( Ebook VBA Excel ) Welcome EVBA.info - Nothing Is Unable ... About Excel Tricks, Learning VBA Programming, Dedicated Software, Free Courses, Living Skills ...

Thursday, May 21, 2020

Real Time Data in Excel

Real Time Data in Excel

real time data in excel

Updated April 2020

Excel does support getting real time data into the worksheet with the RTD function, but this requires programming a COM server with which the RTD function communicates.
Power BI is able to make use of streaming data via API connections or PubNub, but what if I just want to pull some live data directly into a cell?
This article describes a way to get real time data into Excel using HTTP requests.
There is another article that describes how to get real time data into Excel using sockets.
With PyXLL I can do just that. PyXLL is an add-in that allows you to write Python code and use the results in Excel. Not only can you pull in real time data, you can write your own functions, amongst other things, making use of the many Python libraries available.
PyXLL uses a class derived from RTD which when combined with your own Python code, allows you to stream real time data.

Before You Start

You will of course need an installation of Python on your PC that will be used by PyXLL in Excel. Download and install the latest version from here : Get Python - it's free!
Once that's done you can get PyXLL from here : PyXLL, which is available for a 30 day free trial, after that there is a charge for continuing to use it.

Real Time Data

The world is awash with mentions of IoT (Internet of Things) streaming data from devices like sensors that tell you the temperature of the water in your fish tank, to what your cows are doing.
If you had a stream of data from an IoT device you could certainly use the following method to read and present the data in Excel. But in this post I'm going to write code for two examples, the price of crypto currencies, and some random numbers from a web based random number generator.

API's

To get such information it can be made available by what's known as an API - application programming interface. Which is a way of asking for and getting some data from a program, website or some other similar service.
For the crypto currencies, CoinCap provide a simple API which gives you information including the price for the currency you specify.
If I want to get the price of Bitcoin all I need to do is visit https://api.coincap.io/v2/assets/bitcoin
If you click that link, rather than see a nicely laid out website, you'll just see a bunch of text like this
{"data":{"id":"bitcoin","rank":"1","symbol":"BTC","name":"Bitcoin","supply":"18141812.0000000000000000","maxSupply":"21000000.0000000000000000","marketCapUsd":"134720405124.6647178192585068","volumeUsd24Hr":"3323508659.7492937215632559","priceUsd":"7425.9619229140241239","changePercent24Hr":"1.4005161160705637","vwap24Hr":"7370.9732287929146683"},"timestamp":1578185530218}
That's because the website is expecting to be sending the data back to a program so has formatted it into a JSON response the program can easily use. This is not meant for human eyes so isn't meant to look pretty. When we get this data we manipulate it in our Python code and return whatever we want to Excel, which in this case is the price.
If you want to you could make use of all the other information too but I just want the price for this example.
So by making a call to the CoinCap API for each currency I specify, it sends me the informaton related to that crypto currency.
It works the same way for the random number generator. My Python code makes a call to the random number generator at https://random.org, and the website sends me back a random integer.

PyXLL RTD Class

By adding this RTD class into your code and modifying __thread_func to do the work of calling the API's and processing the result, we get our code to update our currency prices and random numbers in real time.

Crypto Currency Price Updates

real time python code for currency updates

Random Number Generator

real time python code for random number generator

Calling the Functions

By defining a function for both of these bits of code through PyXLL, Excel is able to access them like it would any other workbook function.
To get the price for Bitcoin
=coin_price_rtd("bitcoin")
Note that the currency name must be lower case.
You can pass in a cell reference rather than a string if you want to e.g.
=coin_price_rtd(LOWER(A1))
Where A1 contains the currency name.
For the random numbers, use this next function which doesn't require any arguments
=random_rtd()
Here are both functions in action.
Crypto Currency Prices in real time
Random numbers in real time with Python
I'm multiplying the random numbers by a decimal to make them more interesting, and I've added some conditional formatting to make the numbers visually appealing.
The top 50% of the currencies by price are green, the bottom 50% are red. In the workbook you can download just below, I've included just 3 currencies, but I'm sure you can add any others you want.
For the random numbers, any number greater than the average of all the numbers, is green.

Download the Excel Workbook and Python Code

In order to get this to work you'll need an installation of Python and to have installed the PyXLL add-in as described at the top of this article.
Excel Workbook & Python Code Excel-Real-Time-Data.zip

Summary

If you know Python or want to try your hand at it, PyXLL is definitely worth a look. And if you want to get real time data into Excel it looks like a good solution.
Using Python it's also possible to get real time data using sockets, and you can bring real time data into Power BI with PubNUb.
#evba #etipfree #eama #kingexcel
📤How to Download ebooks: https://www.evba.info/2020/02/instructions-for-downloading-documents.html?m=1

No comments:

Post a Comment