VBA & Macro Problems

How to Automatically Record API Stock Prices in Excel Every 5 Minutes

Algirdas JasaitisAlgirdas Jasaitis Oct 8, 2026 869 views

Question details

The user needs to capture continuously updating API stock prices in Excel at regular five-minute intervals using VBA or formulas.

How to Automatically Record API Stock Prices in Excel Every 5 Minutes
Product
Microsoft Excel
Device & OS
not provided
Scenario
Tracking and recording live financial market data received via API at regular intervals.
Observed behavior
The goal is to automatically append or capture each reported stock price into adjacent cells every 5 minutes to build a historical log.
Before you start

Ensure you have the Developer tab enabled in Excel and have saved your workbook as an Excel Macro-Enabled Workbook (.xlsm) so that your automation scripts can run properly.

Solution 1Recommended

Automate Data Capture Using VBA Application.OnTime

Use a VBA macro with the Application.OnTime method to trigger a data recording script repeatedly every 5 minutes.

This method utilizes Excel's built-in timer to execute a specific subroutine at regular intervals. The script reads the live API value from your designated cell and copies it to the next available row in a tracking column, ensuring an ongoing historical log.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications (VBA) editor. Click 'Insert' in the top menu and select 'Module' to create a new script area.

2
Write the Copy Macro

Create a subroutine (e.g., 'RecordPrice') that finds the last used row in your target column, moves one row down, and pastes the current value from your live API cell.

3
Set the Timer Loop

At the end of your 'RecordPrice' subroutine, add the line: Application.OnTime Now + TimeValue("00:05:00"), "RecordPrice". This tells Excel to run the exact same macro again in 5 minutes.

4
Create a Stop Macro

To prevent the macro from running indefinitely, write a separate 'StopTimer' subroutine that uses the Application.OnTime method with the 'Schedule:=False' parameter to cancel the pending execution.

5
Assign Macros to Buttons

Return to your Excel worksheet. Insert two shape buttons (one for Start, one for Stop), right-click them, and choose 'Assign Macro' to link them to your respective VBA subroutines.

Automate Data Capture Using VBA Application.OnTime
Keep Excel Open: For the Application.OnTime loop to continue recording data, your computer must remain awake and the Excel application must stay open.
Free Microsoft Office alternative

Try WPS Office for Seamless Spreadsheet Management

While complex API polling often requires extensive Microsoft-specific frameworks, WPS Office provides an incredibly lightweight, free, and highly compatible alternative for your daily spreadsheet and macro needs. Experience a familiar interface with outstanding support for complex formulas and data processing.

  1. 1. Download the Software: Navigate to the official WPS website and download the free installation package for your operating system.
  2. 2. Install WPS Office: Run the downloaded installer and follow the quick on-screen instructions to complete the setup.
  3. 3. Open Your Spreadsheets: Launch WPS Spreadsheets and open your existing Excel workbooks seamlessly with full formatting retention.
Free and lightweight Office suiteHigh compatibility with Microsoft Excel formats (.xlsx, .xlsm, .csv)Built-in support for standard formulas, charts, and data trackingFamiliar user interface requiring zero learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Can I record historical API stock prices in Excel without using VBA?

Using standard formulas or Power Query alone will typically overwrite the previous data on each refresh. To keep a running historical log without VBA, you would need to use Office Scripts (on Excel for Web) or link your Excel workbook to an external database or flow automation tool like Power Automate.

Does Excel's Application.OnTime run if the computer goes to sleep?

No, the Application.OnTime macro will pause if the computer goes to sleep or if Excel is closed. To ensure continuous data tracking, you must adjust your PC's power settings to prevent sleep mode while the script is active.

How do I stop an Application.OnTime loop once it starts?

You need to run the Application.OnTime method again with the exact same time parameter, but add 'Schedule:=False' at the end of the statement. It is best practice to store the scheduled execution time in a public variable so it can be precisely targeted and canceled by your stop macro.