logo
search
VBA & Macro Problems

How to Create a Day-Night Terminator and Sun Position Chart in Excel VBA

Huma Ashraf ChHuma Ashraf Ch Sep 28, 2026 868 views

Question details

The user wants to develop and troubleshoot an Excel VBA project that calculates and displays an accurate day-night terminator and the Sun's geographic position on a world map.

How to Create an Accurate Day-Night Terminator and Sun Position Chart in Excel VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Creating an advanced VBA-based astronomical chart to dynamically map the sun's position and Earth's day/night shading based on the current time.
Observed behavior
Requires precise debugging of astronomical calculations, terminator longitude mapping, chart coordinate scaling, and continuous animation logic to yield accurate results.
Before you start

Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled the Developer tab in your ribbon to access the Visual Basic Editor.

Solution 1Recommended

Verify and Debug Astronomical Calculations and Mapping Logic

Use this solution to isolate and fix inaccuracies within your VBA math algorithms, chart scaling, and animation loops.

Creating a day-night terminator requires complex mathematical conversions. Issues generally arise from incorrect Solar Declination formulas, misaligned chart axes, or inefficient shading logic. Breaking the code down into testable modules ensures each calculation outputs the correct coordinates before rendering the chart.

1
Validate Solar Position Math

Check your VBA functions for solar declination and equation of time. Compare the outputs of your VBA functions for a specific date and time against trusted astronomical data like the NOAA Solar Calculator.

2
Fix Chart Coordinate Scaling

Ensure your Scatter or Surface chart axes match standard global coordinates (-180 to 180 degrees Longitude, -90 to 90 degrees Latitude). Right-click the chart axes, select 'Format Axis', and set fixed minimum and maximum bounds.

3
Implement Animation Logic

Use the 'Application.OnTime' method in your VBA module to create a loop that recalculates the sun's position every minute and triggers a chart refresh without freezing the Excel interface.

Verify and Debug Astronomical Calculations and Mapping Logic
Performance Tip: Heavy trigonometric calculations and continuous chart refreshes can slow down Excel. Turn off 'Application.ScreenUpdating' during the recalculation phase of your loop to prevent screen flickering.
Develop Advanced VBA Projects

Run and Edit Complex VBA Macros in WPS Spreadsheets

WPS Spreadsheets provides robust support for VBA macros, enabling you to seamlessly run, edit, and troubleshoot complex astronomical calculations and dynamic charts just like you would in Microsoft Excel.

  1. 1. Open WPS Spreadsheets: Launch WPS Office and open your .xlsm file containing the day-night terminator project.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon. If you do not see it, go to Settings to enable Developer tools.
  3. 3. Open the VBA Editor: Click the 'VBA Editor' button. This opens the integrated development environment where you can review your astronomical math functions.
  4. 4. Run and Debug Your Code: Use the built-in debugging tools, such as breakpoints and the Immediate Window, to step through your solar position calculations and execute the chart updates.
Fully compatible with Microsoft Excel VBA syntax, user forms, and modulesHigh-performance calculation engine suitable for heavy trigonometric and astronomical mathSeamlessly opens, edits, and saves .xlsm macro-enabled workbooksFree and lightweight alternative with a highly intuitive user interface
QA img-9

Frequently Asked Questions

Why is my VBA day-night terminator chart not updating in real-time?

Real-time updates require an automated loop. Standard macros run once and stop. You must use the 'Application.OnTime' method or a Windows API timer within your VBA code to schedule the macro to run continuously at specific intervals (e.g., every 60 seconds) to refresh the chart.

How do I correctly calculate solar declination in VBA?

Solar declination varies throughout the year and is typically calculated using the fractional year or Julian Day. You can adapt standard algorithms, such as those provided by the NOAA Solar Calculator, into VBA functions utilizing native trigonometric functions like Sin() and Cos().

How can I plot the day-night shading on an Excel chart?

The most common method is using a Surface Chart or an Area Chart layered over a world map image. Your VBA code must calculate the terminator line's latitude for every degree of longitude and output these values into a worksheet range that the chart references.

Can I run this Excel VBA astronomical model in WPS Office?

Yes, the professional and premium versions of WPS Office fully support VBA. You can open your macro-enabled workbook (.xlsm) in WPS Spreadsheets, access the VBA Editor, and run your astronomical chart animations natively.