How to Collect Real Time Data Automatically (every few seconds) and save in excel column

KenTClark

New Member
Joined
Aug 16, 2022
Messages
10
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hey Everyone,

First off, great discussion and help you guys provide here, cheers to all!!!

I already tried searching for something similar to what I want to do with stock index real time quotes in the forum but unfortunately couldn't find. Its a pretty simple concept I think:

Now that excel has built in real time data for stocks from their cloud business, excel can retrieve updated stock or index prices in a specified cell, every time you hit refresh.

How can I automatically ask excel to record the time and price, daily high, daily low in the rows below, at every second or minute interval ? By 4 pm I would have a dataset for the entire day collected in the same worksheet? (Please see example picture)

Right now I'm just recording the values manually whenever i have time, which is not ideal :) . If this can be automatically recorded in excel in the background that would be great!!! I think the data refresh rate can be changed, instead of having to press the update button, I'm also looking in to that.


I already tried copying a similar program from an (old thread~2010?) into my excel VBA and after enabling macros, excel crashes and i can't even open the test file. So that's why I'm reposting.

"Private Sub Worksheet_Calculate()
capturerow = 2

currow = Range("A65536").End(xlUp).Row

Cells(currow + 1, 1) = Cells(capturerow, 1)
Cells(currow + 1, 2) = Cells(capturerow, 2)
Cells(currow + 1, 3) = Cells(capturerow, 3)
End Sub"


Thank you for your assistance!!
Best Regards,
KC
 

Attachments

  • RealTimeCollect.png
    RealTimeCollect.png
    23.7 KB · Views: 330

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi KenTClark, welcome to the MrExcel Forum...

From this video by Bill Jelen (MrExcel), I was able to alter his code to do other stuff, including how often you want excel to update, and not have to wait for the five minute auto refresh. I have not looked at your requirements I am only trying to show you a path to a more frequent update that may help you.

 
Upvote 0
Solution
Hello ,

Yes that's pretty much what I need Thank you for sharing!!! I'm just stuck on one final thing. I can't get the value from an equation to print in column 5, where i just want to find the difference/Range between the high and the low values . Using the syntax in the video, I'm thinking the equation should be = "=RC[-2] - RC[-1]", but I'm not sure if i have it all correct? I am assuming RC is the current row and column?

Much Appreciated !!
KC

VBA Code:
Sub SaveThePrice()
    Dim WS As Worksheet
    Set WS = ThisWorkbook.Worksheets(1)
    WS.Cells(1, 1).RefreshLinkedDataType
    DoEvents
    WS.Cells(2, 1).Value = Now
    NextRow = WS.Cells(Rows.Count, 1).End(xlUp).Row + 1
    WS.Cells(NextRow, 1).Resize(1, 4).Value = Array(WS.Cells(2, 1).Value, WS.Cells(2, 2).Value, WS.Cells(2, 3).Value, WS.Cells(2, 4).Value)
    WS.Cells(NextRow, 5).FormulaRange = "=RC[-2]-RC[-1][B]"[/B]
    If WS.Cells(1, 9).Value = True Then
        Exit Sub
    Else
    Application.OnTime Now + TimeValue("00:01:00"), "SaveThePrice"
    End If
    
End Sub
 

Attachments

  • UpdatePrice.jpg
    UpdatePrice.jpg
    122.2 KB · Views: 143
Upvote 0
Try This...

VBA Code:
Sub SaveThePrice()

        Dim WS As Worksheet, NextRow As Long
        
        Set WS = ThisWorkbook.Worksheets(1)
        WS.Cells(1, 1).RefreshLinkedDataType
        DoEvents
        WS.Cells(2, 1).Value = Now
        NextRow = WS.Cells(Rows.Count, 1).End(xlUp).Row + 1
        WS.Cells(NextRow, 1).Resize(1, 4).Value = Array(WS.Cells(2, 1).Value, WS.Cells(2, 2).Value, WS.Cells(2, 3).Value, WS.Cells(2, 4).Value)
        WS.Cells(NextRow, 5) = Cells(NextRow, 3) - Cells(NextRow, 4)
        If WS.Cells(1, 9).Value = True Then
            Exit Sub
        Else
        Application.OnTime Now + TimeValue("00:01:00"), "SaveThePrice"
        End If
    
End Sub
 
Upvote 0
yes it works now :cool: (y)

however, there's a new issue with the macro running on its own for some reason. After I run it and one line is printed and waiting for the next line, it stops and an error pops up:

cannot run the macro "https://d.doc.live.net....etc.etc filename.xlsm'savetheprice'. The macro may not be available in this workbook or all macros maybe disabled.

Come to think of it, it never got to line 2 yesterday either. I just had to manually restart the macro to get the next line to print. Any insight into why this is happening?

Thanks again for your assistance! Cheers

Kind Regards,
KC
 
Upvote 0
I have re-opened my workbook that I created for you. It is running fine. I am going to let it update for three minutes and post it.

Stock Prices.xlsm
ABCDEFGH
1S&P 500 INDEXreal time pricehighlowRun
28/17/2022 13:23$ 4,272.44$ 4,282.37$ 4,253.08
3
4timepricehighlowRange
58/17/2022 9:324305.24325.284277.7747.51
68/17/2022 9:334305.24325.284277.7747.51
78/17/2022 9:344305.24325.284277.7747.51
88/17/2022 13:214273.164282.374253.0829.29
98/17/2022 13:224272.334282.374253.0829.29
108/17/2022 13:234272.444282.374253.0829.29
11
Sheet1
Cell Formulas
RangeFormula
B2B2=A1.Price
C2C2=A1.High
D2D2=A1.Low


Are the macros enabled. You may want to close and reopen...
 
Upvote 0
Just for the heck of it, as it seemed kind of redundant to be looking at all the duplicate entries when the spread between the High/Low has not changed, I added a line to the code to remove those duplicate spreads. I don't know if you need to see the "Every Minute" data but here it is...
VBA Code:
Sub SaveThePrice()

        Dim WS As Worksheet, NextRow As Long
        
        Set WS = ThisWorkbook.Worksheets(1)
        WS.Cells(1, 1).RefreshLinkedDataType
        DoEvents
        WS.Cells(2, 1).Value = Now
        NextRow = WS.Cells(Rows.Count, 1).End(xlUp).Row + 1
        WS.Cells(NextRow, 1).Resize(1, 4).Value = Array(WS.Cells(2, 1).Value, WS.Cells(2, 2).Value, WS.Cells(2, 3).Value, WS.Cells(2, 4).Value)
        WS.Cells(NextRow, 5) = Cells(NextRow, 3) - Cells(NextRow, 4)
        ActiveSheet.Range("A4:E" & NextRow).RemoveDuplicates Columns:=5, Header:=xlYes  'Remove Dupes
        If WS.Cells(1, 9).Value = True Then
            Exit Sub
        Else
        Application.OnTime Now + TimeValue("00:01:00"), "SaveThePrice"
        End If
    
End Sub
Stock Prices.xlsm
ABCDEFGH
1S&P 500 INDEXreal time pricehighlowRun
28/17/2022 13:38$ 4,276.24$ 4,282.37$ 4,253.08
3
4timepricehighlowRange
58/17/2022 9:324305.24325.284277.7747.51
68/17/2022 13:214273.164282.374253.0829.29
7
8
9
Sheet1
Cell Formulas
RangeFormula
B2B2=A1.Price
C2C2=A1.High
D2D2=A1.Low
 
Upvote 0
Hi igold,

Thanks for helping out. I don't think there is anything wrong with the code it is essentially same as what you have there and I have macros enabled when the file is opened. Like I mentioned, the macros runs and prints one row successfully but does not do it the second time . It just gives that error and I have to manually click run in the macros windows for it to "restart", so each line you see in my screen shot I had to manually run the macros. I chose different macros security settings and its still giving the same problem . I'm not sure where else i can change macros settings or security.

Thanks,
KC
 

Attachments

  • RealTimeCollect.png
    RealTimeCollect.png
    85.3 KB · Views: 61
Upvote 0
Hi igold,

Thanks for helping out. I don't think there is anything wrong with the code it is essentially same as what you have there and I have macros enabled when the file is opened. Like I mentioned, the macros runs and prints one row successfully but does not do it the second time . It just gives that error and I have to manually click run in the macros windows for it to "restart", so each line you see in my screen shot I had to manually run the macros. I chose different macros security settings and its still giving the same problem . I'm not sure where else i can change macros settings or security.

Thanks,
KC
also here is the code i copied right from the VBA window
VBA Code:
Sub SaveThePrice()
    Dim WS As Worksheet
    Set WS = ThisWorkbook.Worksheets(1)
    WS.Cells(1, 1).RefreshLinkedDataType
    DoEvents
    WS.Cells(2, 1).Value = Now
    Nextrow = WS.Cells(Rows.Count, 1).End(xlUp).Row + 1
    WS.Cells(Nextrow, 1).Resize(1, 4).Value = Array(WS.Cells(2, 1).Value, WS.Cells(2, 2).Value, WS.Cells(2, 3).Value, WS.Cells(2, 4).Value)
    WS.Cells(Nextrow, 5) = Cells(Nextrow, 3) - Cells(Nextrow, 4)
    If WS.Cells(1, 9).Value = True Then
        Exit Sub
    Else
    Application.OnTime Now + TimeValue("00:00:30"), "SaveThePrice"
    End If
    
End Sub
 
Upvote 0
I
Hi igold,

Thanks for helping out. I don't think there is anything wrong with the code it is essentially same as what you have there and I have macros enabled when the file is opened. Like I mentioned, the macros runs and prints one row successfully but does not do it the second time . It just gives that error and I have to manually click run in the macros windows for it to "restart", so each line you see in my screen shot I had to manually run the macros. I chose different macros security settings and its still giving the same problem . I'm not sure where else i can change macros settings or security.

Thanks,
KC
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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