Capture live dynamic data as a static value at a specific time

brutusmc99

Board Regular
Joined
Oct 6, 2014
Messages
113
Hi, is there a way to grab live dynamic stock price data from column A at specific time intervals? For example, I would like to grab the price data every 15 minutes and place each value in its own column, so columns B+n? So, the price at 9:30 am would be in B, the price at 9:45 would be in C, and so on. Of course, the grabbed values should remain static in their respective columns. Any help would be appreciated. Thank you.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
OK, here goes, you asked for it!

I created a dummy worksheet with two named ranges that the macro uses. CurrentStockPriceRng is the range with the stock prices. StockTimes is the row of times you want to save your static stock prices. The macro looks at the times and checks if the columns are empty and the time is in the future. It then schedules StockStatic SUB to get the current stock prices and copie them to the correct column. Then repeat. Ignore the formulas with the dummy stock prices, that was just for testing.


Cell Formulas
RangeFormula
C1:M1C1=+B1+0.000694444444444442
A2A2=146.52+SECOND(NOW())
A3A3=200.14+SECOND(NOW())
A4A4=300.21+SECOND(NOW())
A5A5=51+SECOND(NOW())
A6A6=94.1+SECOND(NOW())
A7A7=14.1+SECOND(NOW())
A8A8=100.41+SECOND(NOW())
Named Ranges
NameRefers ToCells
StockTimes='Stock Price Data'!$B$1:$M$1C1


Put this code in a standard VBA module
VBA Code:
Public SavedTime As Date
Public TimeRng As Range
Public TimeChosen As Range
Public StockSht As Worksheet


Sub StaticStockPrices()
  Dim Cel As Range
  
  Set StockSht = Sheets("Stock Price Data")
  Set TimeRng = StockSht.Range("StockTimes")
  
                          'Look for time with empty values below
  For Each Cel In TimeRng
    Debug.Print Format(Cel.Value, "hh:mm")
    If Cel.Offset(1, 0) = "" And Int(Now()) + Cel.Value > Now() Then   'must be after current time
      SavedTime = Int(Now()) + Cel.Value
      Set TimeChosen = Cel
      Exit For
    End If
  Next Cel
  
  If SavedTime > 0 Then
    Application.OnTime SavedTime, "StockStatic", schedule:=True
  End If
  
End Sub


Sub StockStatic()

  Dim OutRng As Range
  Dim StockRng As Range
  
  If SavedTime = 0 Then Exit Sub        'VBA was reset, variables zeroed
  'Application.OnTime SavedTime, "StockStatic", schedule:=False
  
  Set StockRng = StockSht.Range("CurrentStockPriceRng")   'Named Range you create
  Set OutRng = StockSht.Range(TimeChosen.Offset(1, 0), TimeChosen.Offset(StockRng.Rows.Count, 0))
  OutRng.Value = StockRng.Value     'Copy to next time
  
  SavedTime = 0
    
  Call StaticStockPrices      'Repeat
  
End Sub

Sub StopStockStatic()
  On Error Resume Next
  Application.OnTime SavedTime, "StockStatic", schedule:=False
  On Error GoTo 0

End Sub

Put this code in ThisWorkbook module
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  On Error Resume Next
  Application.OnTime SavedTime, "StockStatic", schedule:=False
  On Error GoTo 0
End Sub
 
Upvote 0
OK, here goes, you asked for it!

I created a dummy worksheet with two named ranges that the macro uses. CurrentStockPriceRng is the range with the stock prices. StockTimes is the row of times you want to save your static stock prices. The macro looks at the times and checks if the columns are empty and the time is in the future. It then schedules StockStatic SUB to get the current stock prices and copie them to the correct column. Then repeat. Ignore the formulas with the dummy stock prices, that was just for testing.


Cell Formulas
RangeFormula
C1:M1C1=+B1+0.000694444444444442
A2A2=146.52+SECOND(NOW())
A3A3=200.14+SECOND(NOW())
A4A4=300.21+SECOND(NOW())
A5A5=51+SECOND(NOW())
A6A6=94.1+SECOND(NOW())
A7A7=14.1+SECOND(NOW())
A8A8=100.41+SECOND(NOW())
Named Ranges
NameRefers ToCells
StockTimes='Stock Price Data'!$B$1:$M$1C1


Put this code in a standard VBA module
VBA Code:
Public SavedTime As Date
Public TimeRng As Range
Public TimeChosen As Range
Public StockSht As Worksheet


Sub StaticStockPrices()
  Dim Cel As Range
 
  Set StockSht = Sheets("Stock Price Data")
  Set TimeRng = StockSht.Range("StockTimes")
 
                          'Look for time with empty values below
  For Each Cel In TimeRng
    Debug.Print Format(Cel.Value, "hh:mm")
    If Cel.Offset(1, 0) = "" And Int(Now()) + Cel.Value > Now() Then   'must be after current time
      SavedTime = Int(Now()) + Cel.Value
      Set TimeChosen = Cel
      Exit For
    End If
  Next Cel
 
  If SavedTime > 0 Then
    Application.OnTime SavedTime, "StockStatic", schedule:=True
  End If
 
End Sub


Sub StockStatic()

  Dim OutRng As Range
  Dim StockRng As Range
 
  If SavedTime = 0 Then Exit Sub        'VBA was reset, variables zeroed
  'Application.OnTime SavedTime, "StockStatic", schedule:=False
 
  Set StockRng = StockSht.Range("CurrentStockPriceRng")   'Named Range you create
  Set OutRng = StockSht.Range(TimeChosen.Offset(1, 0), TimeChosen.Offset(StockRng.Rows.Count, 0))
  OutRng.Value = StockRng.Value     'Copy to next time
 
  SavedTime = 0
   
  Call StaticStockPrices      'Repeat
 
End Sub

Sub StopStockStatic()
  On Error Resume Next
  Application.OnTime SavedTime, "StockStatic", schedule:=False
  On Error GoTo 0

End Sub

Put this code in ThisWorkbook module
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  On Error Resume Next
  Application.OnTime SavedTime, "StockStatic", schedule:=False
  On Error GoTo 0
End Sub


Hi, thank you for the quick reply. I really appreciate it. I might have a few follow-up questions after I try and apply it. Hope that's okay. Cheers.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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