VBA & Macro Problems

How to Create an Excel VBA Chart with Celsius and Fahrenheit Y-Axes

Aamir Naveed AkramAamir Naveed Akram Oct 23, 2026 870 views

Question details

The user needs to create a chart in Excel using VBA that correctly displays and aligns Celsius and Fahrenheit temperature scales on the primary and secondary Y-axes.

Create an Excel VBA Chart with Celsius and Fahrenheit Y-Axes
Product
Microsoft Excel
Device & OS
not provided
Scenario
Automating the generation of a dual-scale temperature chart for reports that require both metric and imperial units.
Observed behavior
A VBA script is required to calculate the correct axis bounds (MinimumScale and MaximumScale) so that the primary (Celsius) and secondary (Fahrenheit) gridlines align perfectly.
Before you start

Ensure that the Developer tab is enabled in your ribbon to access the VBA Editor, and verify that your source data is organized with clear headers before running any macro.

Solution 1Recommended

Use VBA to Calculate and Align the Secondary Axis

Write a VBA macro that mathematically links the primary Celsius axis limits to the secondary Fahrenheit axis limits to ensure the gridlines align.

To make a dual-axis chart look professional, the gridlines for both scales must match. Since Fahrenheit equals Celsius multiplied by 1.8 plus 32, we can use VBA to read the primary axis bounds, apply this formula, and set the secondary axis bounds accordingly.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. Go to Insert > Module to create a new blank script area.

2
Define the Chart Object

Write a macro to set your active chart as a variable. Use code like 'Set cht = ActiveChart' to target the chart you want to modify.

3
Read Primary Axis (Celsius) Bounds

Extract the MinimumScale and MaximumScale values from the primary Y-axis (xlValue, xlPrimary) and store them as variables in your script.

4
Calculate and Apply Secondary Axis (Fahrenheit) Bounds

Enable the secondary axis using 'HasAxis(xlValue, xlSecondary) = True'. Set its MinimumScale to '(CelsiusMin * 1.8) + 32' and MaximumScale to '(CelsiusMax * 1.8) + 32'.

5
Run the Macro

Close the VBA editor, select your chart in the spreadsheet, and press Alt + F8 to run your new macro. The axes will instantly align.

Use VBA to Calculate and Align the Secondary Axis
Tip: To keep the gridlines perfectly synchronized, ensure the MajorUnit of the secondary axis is also set to the primary MajorUnit multiplied by 1.8.
Advanced Charting in WPS Spreadsheet

Automate Your Charts Seamlessly with WPS Office

WPS Spreadsheet offers comprehensive support for VBA and macros, allowing you to execute complex scripts for dual-axis charts just like Microsoft Excel, but in a more lightweight and efficient environment.

  1. 1. Open Your Data in WPS: Launch WPS Spreadsheet and open your workbook containing the temperature data.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon and click on 'Visual Basic' to open the VBA editor.
  3. 3. Paste and Run Code: Paste your axis-alignment VBA code into a new module and execute it to automatically format your dual-axis chart.
Complete support for VBA macros and Excel (.xlsm) macro-enabled files.Intuitive chart formatting options for handling primary and secondary axes.High compatibility with Microsoft Office file formats like .xlsx and .csv.Lightweight application that is free to download and easy to use.
microsoft office alternative - wps office

Frequently Asked Questions

Why are my primary and secondary axis gridlines not aligning?

Gridlines fail to align when the mathematical ratio between the primary and secondary axis bounds (Minimum, Maximum, and Major Unit) does not match. For temperature, the secondary bounds must strictly equal the primary bounds multiplied by 1.8, plus 32.

How do I show a secondary Y-axis in my Excel chart?

To display a secondary Y-axis, you must first assign a data series to it. Right-click the specific data series in your chart, choose 'Format Data Series', and select 'Secondary Axis' from the Series Options panel.

Does WPS Spreadsheet support Excel VBA macros?

Yes, WPS Spreadsheet supports VBA (Visual Basic for Applications). You can write, edit, and run macros to automate chart creation and data processing just as you would in Microsoft Excel.