How to Make Square Grid Lines in an Excel Graph (4 Ways)

The following dataset is a Product Delivery Report. We will use this dataset to generate an Excel Graph.

This graph has the traditional rectangular grid. We will use this graph to make square grid lines in an Excel graph.


Method 1 – Changing Axis Scales to Make Square Grid Lines in a Graph

Steps:

  • Press ALT + F11 to open the VBA Editor.
  • Go to Insert āž¤ Module to create a new module.

  • Insert the following code in the newly opened module:
Sub Changing_Axis_Scales()

Ā  If Not ActiveChart Is Nothing Then
Ā Ā Ā  ChangingAxisScales ActiveChart

Ā  ElseIf TypeName(Selection) = "DrawingObjects" Then
Ā Ā Ā  Dim zshp As Shape
Ā Ā Ā  For Each zshp In Selection.ShapeRange
Ā Ā Ā Ā Ā  If zshp.HasChart Then
Ā Ā Ā Ā Ā Ā Ā  ChangingAxisScales zshp.Chart
Ā Ā Ā Ā Ā  End If
Ā Ā Ā  Next

Ā  Else
Ā Ā Ā  MsgBox "No Chart selected. Please select at least one.", vbExclamation, "Warning!"
Ā  End If

End Sub

Function ChangingAxisScales(zChart As Chart)

Ā  With zChart
Ā Ā Ā  With .PlotArea
Ā Ā Ā Ā Ā  Dim xplotInHt As Double, xplotInWd As Double
Ā Ā Ā Ā Ā  xplotInHt = .InsideHeight
Ā Ā Ā Ā Ā  xplotInWd = .InsideWidth
Ā Ā Ā  End With

Ā Ā Ā  With .Axes(xlValue)
Ā Ā Ā Ā Ā  Dim Ymax1 As Double, Ymin1 As Double, Ymaj1 As Double
Ā Ā Ā Ā Ā  Ymax1 = .MaximumScale
Ā Ā Ā Ā Ā  Ymin1 = .MinimumScale
Ā Ā Ā Ā Ā  Ymaj1 = .MajorUnit
Ā Ā Ā Ā Ā  .MaximumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MinimumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MajorUnitIsAuto = False
Ā Ā Ā  End With

Ā Ā Ā  With .Axes(xlCategory)
Ā Ā Ā Ā Ā  Dim Xmax1 As Double, Xmin1 As Double, Xmaj1 As Double
Ā Ā Ā Ā Ā  Xmax1 = .MaximumScale
Ā Ā Ā Ā Ā  Xmin1 = .MinimumScale
Ā Ā Ā Ā Ā  Xmaj1 = .MajorUnit
Ā Ā Ā Ā Ā  .MaximumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MinimumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MajorUnitIsAuto = False
Ā Ā Ā  End With

Ā Ā Ā  Dim Ytic1 As Double, Xtic1 As Double
Ā Ā Ā  Ytic1 = xplotInHt * Ymaj1 / (Ymax1 - Ymin1)
Ā Ā Ā  Xtic1 = xplotInWd * Xmaj1 / (Xmax1 - Xmin1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā  If Xtic1 > Ytic1 Then
Ā Ā Ā Ā Ā  .Axes(xlCategory).MaximumScale = xplotInWd * Xmaj1 / Ytic1 + Xmin1
Ā Ā Ā  Else
Ā Ā Ā Ā Ā  .Axes(xlValue).MaximumScale = xplotInHt * Ymaj1 / Xtic1 + Ymin1
Ā Ā Ā  End If
Ā  End With

End Function

Changing Axis Scales to Make Square Grid Lines in Excel Graph

  • Go back to your worksheet and click on your Excel graph to select it.
  • Press ALT + F5Ā to open the Macro window.
  • Select the macro Changing_Axis_Scales and hit the Run button.

Changing Axis Scales to Make Square Grid Lines in Excel Graph

The VBA code will run. The code will make your rectangular grid lines square, just like the picture below:

Changing Axis Scales to Make Square Grid Lines in Excel Graph


Method 2 – Forcing Equal Major Unit Spacing for Square Grid Lines

Steps:

  • Press ALT + F11 to open the VBA Editor.
  • Go to Insert āž¤ Module to create a new module.

  • Insert the following code in the newly opened module:
Sub Forcing_Equal_Major_Unit_Spacing()

Ā  If Not ActiveChart Is Nothing Then
Ā Ā Ā  ForcingEqualMajorUnitSpacing ActiveChart, True

Ā Ā  ElseIf TypeName(Selection) = "DrawingObjects" Then
Ā Ā Ā  Dim zshp As Shape
Ā Ā Ā  For Each zshp In Selection.ShapeRange
Ā Ā Ā Ā Ā  If zshp.HasChart Then
Ā Ā Ā Ā Ā Ā Ā  ForcingEqualMajorUnitSpacing zshp.Chart
Ā Ā Ā Ā Ā  End If
Ā Ā Ā  Next

Ā  Else
Ā Ā Ā  MsgBox "No Chart selected. Please select at least one.", vbExclamation, "Warning!"
Ā  End If

End Sub

Function ForcingEqualMajorUnitSpacing(xChart As Chart, Optional xEqualMajorUnit As Boolean = False)

Ā  With xChart
Ā Ā Ā  With .PlotArea
Ā Ā Ā Ā Ā  Dim plotInHt1 As Double, plotInWd1 As Double
Ā Ā Ā Ā Ā  plotInHt1 = .InsideHeight
Ā Ā Ā Ā Ā  plotInWd1 = .InsideWidth
Ā Ā Ā  End With
Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā  With .Axes(xlValue)
Ā Ā Ā Ā Ā  Dim Ymax1 As Double, Ymin1 As Double, Ymaj1 As Double
Ā Ā Ā Ā Ā  Ymax1 = .MaximumScale
Ā Ā Ā Ā Ā  Ymin1 = .MinimumScale
Ā Ā Ā Ā Ā  Ymaj1 = .MajorUnit
Ā Ā Ā Ā Ā  .MaximumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MinimumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MajorUnitIsAuto = False
Ā Ā Ā  End With

Ā Ā Ā  With .Axes(xlCategory)
Ā Ā Ā Ā Ā  Dim Xmax1 As Double, Xmin1 As Double, Xmaj1 As Double
Ā Ā Ā Ā Ā  Xmax1 = .MaximumScale
Ā Ā Ā Ā Ā  Xmin1 = .MinimumScale
Ā Ā Ā Ā Ā  Xmaj1 = .MajorUnit
Ā Ā Ā Ā Ā  .MaximumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MinimumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MajorUnitIsAuto = False
Ā Ā Ā  End With

Ā Ā Ā  If xEqualMajorUnit Then
Ā Ā Ā Ā Ā  Xmaj1 = WorksheetFunction.Min(Xmaj1, Ymaj1)
Ā Ā Ā Ā Ā  Ymaj1 = Xmaj1
Ā Ā Ā Ā Ā  .Axes(xlCategory).MajorUnit = Xmaj1
Ā Ā Ā Ā Ā  .Axes(xlValue).MajorUnit = Ymaj1
Ā Ā Ā  End If
Ā Ā Ā Ā Ā 
Ā Ā Ā  Dim Ytic1 As Double, Xtic1 As Double
Ā Ā Ā  Ytic1 = plotInHt1 * Ymaj1 / (Ymax1 - Ymin1)
Ā Ā Ā  Xtic1 = plotInWd1 * Xmaj1 / (Xmax1 - Xmin1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā  If Xtic1 > Ytic1 Then
Ā Ā Ā Ā Ā  .Axes(xlCategory).MaximumScale = plotInWd1 * Xmaj1 / Ytic1 + Xmin1
Ā Ā Ā  Else
Ā Ā Ā Ā Ā  .Axes(xlValue).MaximumScale = plotInHt1 * Ymaj1 / Xtic1 + Ymin1
Ā Ā Ā  End If
Ā  End With

End Function

Forcing Equal Major Unit Spacing to Make Square Grid Lines in Excel Graph

  • Go back to your worksheet and click on your Excel graph to select it.
  • Press ALT + F5Ā to open the Macro window.
  • Select the macro Forcing_Equal_Major_Unit_Spacing and hit the Run button.

The VBA code will run. The code will make your rectangular grid lines square, just like the picture below:

Forcing Equal Major Unit Spacing to Make Square Grid Lines in Excel Graph

Read More: How to Adjust Gridlines in Excel ChartĀ 


Method 3 – Creating Square Grid Lines by Changing Plot Area Size

Steps:

  • Press ALT + F11 to open the VBA Editor.
  • Go to Insert āž¤ Module to create a new module.

  • Insert the following code in the newly opened module:
Sub Changing_Plot_Area_Size()

Ā  If Not ActiveChart Is Nothing Then
Ā Ā Ā  ChangingPlotAreaSize ActiveChart, True

Ā  ElseIf TypeName(Selection) = "DrawingObjects" Then
Ā Ā Ā  Dim zshp As Shape
Ā Ā Ā  For Each zshp In Selection.ShapeRange
Ā Ā Ā Ā Ā  If zshp.HasChart Then
Ā Ā Ā Ā Ā Ā Ā  ChangingPlotAreaSize zshp.Chart
Ā Ā Ā Ā Ā  End If
Ā Ā Ā  Next

Ā  Else
Ā Ā Ā  MsgBox "No Chart selected. Please select at least one.", vbExclamation, "Warning!"
Ā  End If

End Sub

Function ChangingPlotAreaSize(xChart As Chart, Optional xEqualMajorUnit As Boolean = False)

Ā  With xChart
Ā Ā Ā  With .PlotArea
Ā Ā Ā Ā Ā  Dim plotInHt1 As Double, plotInWd1 As Double
Ā Ā Ā Ā Ā  plotInHt1 = .InsideHeight
Ā Ā Ā Ā Ā  plotInWd1 = .InsideWidth
Ā Ā Ā  End With
Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā  With .Axes(xlValue)
Ā Ā Ā Ā Ā  Dim Ymax1 As Double, Ymin1 As Double, Ymaj1 As Double
Ā Ā Ā Ā Ā  Ymax1 = .MaximumScale
Ā Ā Ā Ā Ā  Ymin1 = .MinimumScale
Ā Ā Ā Ā Ā  Ymaj1 = .MajorUnit
Ā Ā Ā Ā Ā  .MaximumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MinimumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MajorUnitIsAuto = False
Ā Ā Ā  End With

Ā Ā Ā  With .Axes(xlCategory)
Ā Ā Ā Ā Ā  Dim Xmax1 As Double, Xmin1 As Double, Xmaj1 As Double
Ā Ā Ā Ā Ā  Xmax1 = .MaximumScale
Ā Ā Ā Ā Ā  Xmin1 = .MinimumScale
Ā Ā Ā Ā Ā  Xmaj1 = .MajorUnit
Ā Ā Ā Ā Ā  .MaximumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MinimumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MajorUnitIsAuto = False
Ā Ā Ā  End With

Ā  Ā Ā If xEqualMajorUnit Then
Ā Ā Ā Ā Ā  Xmaj1 = WorksheetFunction.Min(Xmaj1, Ymaj1)
Ā Ā Ā Ā Ā  Ymaj1 = Xmaj1
Ā Ā Ā Ā Ā  .Axes(xlCategory).MajorUnit = Xmaj1
Ā Ā Ā Ā Ā  .Axes(xlValue).MajorUnit = Ymaj1
Ā Ā Ā  End If
Ā Ā Ā Ā Ā 
Ā Ā Ā  Dim Ytic1 As Double, Xtic1 As Double
Ā Ā Ā  Ytic1 = plotInHt1 * Ymaj1 / (Ymax1 - Ymin1)
Ā Ā Ā  Xtic1 = plotInWd1 * Xmaj1 / (Xmax1 - Xmin1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā  If Xtic1 < Ytic1 Then
Ā Ā Ā Ā Ā  .PlotArea.InsideHeight = .PlotArea.InsideHeight * Xtic1 / Ytic1
Ā Ā Ā Ā Ā  .PlotArea.Top = .PlotArea.Top + _
Ā Ā Ā Ā Ā Ā Ā  (.ChartArea.Height - .PlotArea.Height - .PlotArea.Top) / 2
Ā Ā Ā  Else
Ā Ā Ā Ā Ā  .PlotArea.InsideWidth = .PlotArea.InsideWidth * Ytic1 / Xtic1
Ā Ā Ā Ā Ā  .PlotArea.Left = .PlotArea.Left + _
Ā Ā Ā Ā Ā Ā Ā  (.ChartArea.Width - .PlotArea.Width - .PlotArea.Left) / 2
Ā Ā Ā  End If
Ā  End With

End Function

Changing Plot Area Size to Make Square Grid Lines in Excel Graph

  • Go back to your worksheet and click on your Excel graph to select it.
  • Press ALT + F5Ā to open the Macro window.
  • Select the macro Changing_Plot_Area_Size and hit the Run button.

Changing Plot Area Size to Make Square Grid Lines in Excel Graph

The VBA code will run. The code will make your rectangular grid lines square, just like the picture below:

Changing Plot Area Size to Make Square Grid Lines in Excel Graph


Method 4 – Changing Chart Size to Make Square Grid Lines

Steps:

  • Press ALT + F11 to open the VBA Editor.
  • Go to Insert āž¤ Module to create a new module.

  • Insert the following code in the newly opened module:
Sub Changing_Chart_Size()

Ā  If Not ActiveChart Is Nothing Then
Ā Ā Ā  ChangingChartSize ActiveChart, True, True

Ā  ElseIf TypeName(Selection) = "DrawingObjects" Then
Ā Ā Ā  Dim zshp As Shape
Ā Ā Ā  For Each zshp In Selection.ShapeRange
Ā Ā Ā Ā  Ā If zshp.HasChart Then
Ā Ā Ā Ā Ā Ā Ā  ChangingChartSize zshp.Chart, True, True
Ā Ā Ā Ā Ā  End If
Ā Ā Ā  Next

Ā  Else
Ā Ā Ā  MsgBox "No Chart selected. Please select at least one.", vbExclamation, "Warning!"
Ā  End If

End Sub

Function ChangingChartSize(xChart As Chart, xShrinkChart As Boolean, _
Ā Ā Ā  Optional xEqualMajorUnit As Boolean = False)

Ā  With xChart
Ā Ā Ā  With .PlotArea
Ā Ā Ā Ā Ā  Dim plotInHt1 As Double, plotInWd1 As Double
Ā Ā Ā Ā Ā  plotInHt1 = .InsideHeight
Ā Ā Ā Ā Ā  plotInWd1 = .InsideWidth
Ā Ā Ā  End With
Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā  With .Axes(xlValue)
Ā Ā Ā Ā Ā  Dim Ymax1 As Double, Ymin1 As Double, Ymaj1 As Double
Ā Ā Ā Ā Ā  Ymax1 = .MaximumScale
Ā Ā Ā Ā Ā  Ymin1 = .MinimumScale
Ā Ā Ā Ā Ā  Ymaj1 = .MajorUnit
Ā Ā Ā Ā Ā  .MaximumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MinimumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MajorUnitIsAuto = False
Ā Ā Ā  End With

Ā Ā Ā  With .Axes(xlCategory)
Ā Ā Ā Ā Ā  Dim Xmax1 As Double, Xmin1 As Double, Xmaj1 As Double
Ā Ā Ā Ā Ā  Xmax1 = .MaximumScale
Ā Ā Ā Ā Ā  Xmin1 = .MinimumScale
Ā Ā Ā Ā Ā  Xmaj1 = .MajorUnit
Ā Ā Ā Ā Ā  .MaximumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MinimumScaleIsAuto = False
Ā Ā Ā Ā Ā  .MajorUnitIsAuto = False
Ā Ā Ā  End With

Ā Ā Ā  If xEqualMajorUnit Then
Ā Ā Ā Ā Ā  Xmaj1 = WorksheetFunction.Min(Xmaj1, Ymaj1)
Ā Ā Ā Ā Ā  Ymaj1 = Xmaj1
Ā Ā Ā Ā Ā  .Axes(xlCategory).MajorUnit = Xmaj1
Ā Ā Ā Ā Ā  .Axes(xlValue).MajorUnit = Ymaj1
Ā Ā Ā  End If
Ā Ā Ā Ā Ā 
Ā Ā Ā  Dim Ytic1 As Double, Xtic1 As Double
Ā Ā Ā  Ytic1 = plotInHt1 * Ymaj1 / (Ymax1 - Ymin1)
Ā Ā Ā  Xtic1 = plotInWd1 * Xmaj1 / (Xmax1 - Xmin1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā  If xShrinkChart Then
Ā Ā Ā Ā Ā  If Xtic1 < Ytic1 Then
Ā Ā Ā Ā Ā Ā Ā  .Parent.Height = .Parent.Height - .PlotArea.InsideHeight * (1 - Xtic1 / Ytic1)
Ā Ā Ā Ā Ā  Else
Ā Ā Ā Ā Ā Ā Ā  .Parent.Width = .Parent.Width - .PlotArea.InsideWidth * (1 - Ytic1 / Xtic1)
Ā Ā Ā Ā Ā  End If

Ā Ā Ā  Else
Ā Ā Ā Ā Ā  If Xtic1 < Ytic1 Then
Ā Ā Ā Ā Ā Ā Ā  .PlotArea.InsideHeight = .PlotArea.InsideHeight * Xtic1 / Ytic1
Ā Ā Ā Ā Ā Ā Ā  .PlotArea.Top = .PlotArea.Top + _
Ā Ā Ā Ā Ā Ā Ā Ā Ā  (.ChartArea.Height - .PlotArea.Height - .PlotArea.Top) / 2
Ā Ā Ā Ā Ā  Else
Ā Ā Ā Ā Ā Ā Ā  .PlotArea.InsideWidth = .PlotArea.InsideWidth * Ytic1 / Xtic1
Ā Ā Ā Ā Ā Ā Ā  .PlotArea.Left = .PlotArea.Left + _
Ā Ā Ā Ā Ā Ā Ā Ā Ā  (.ChartArea.Width - .PlotArea.Width - .PlotArea.Left) / 2
Ā Ā Ā Ā Ā  End If
Ā Ā Ā  End If
Ā  End With

End Function

Changing Chart Size to Make Square Grid Lines in Excel Graph

  • Go back to your worksheet and click on your Excel graph to select it.
  • Press ALT + F5Ā to open the Macro window.
  • Select the macro Changing_Chart_Size and hit the Run button.

The VBA code will run. The code will make your rectangular grid lines square, just like the picture below:

Changing Chart Size to Make Square Grid Lines in Excel Graph


Things to Remember

  • All 4 codes given in this article can perfectly make square grid lines when the values of both the X and Y axes are close to each other.
  • If the value of both X & Y axes differs much, then set manual values instead of using the MaximumScale & MinimumScale properties. Otherwise, the final result will be unusable.

Practice Section

At the end of the provided Excel file, you will get an Excel sheet like the following screenshot, where you can practice all the topics discussed in this article.


Download the Practice Workbook

You can download the Excel file from the following link and practice.


Related Articles


<< Go Back To Gridlines in Excel Chart | Excel Chart ElementsĀ |Ā Excel ChartsĀ |Ā Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Mrinmoy Roy
Mrinmoy Roy

Mrinmoy Roy, a dedicated professional with a BSc in Electronics and Communication Engineering from Khulna University of Engineering & Technology, Bangladesh, brings over two years of expertise to the ExcelDemy project. As a prolific contributor, he has authored around 180 articles, showcasing his deep knowledge and passion for Microsoft Excel, Data Analysis, and VBA. His unwavering commitment to continuous learning, combined with versatile skills, renders him well-suited for roles in data management and spreadsheet solutions. He has interest... Read Full Bio

2 Comments
  1. Will this work with Microsoft office non commercial use 2010. When I try your instructions the layout looks different. I managed to

    Thanks for some help if yo have time.

    • Hello RAY,
      Can you please elaborate what problems you are facing? And the problems in getting the output using these methods? I mean, the output scenario needs to be known and analyze properly to make a solution of your problem.

      Thanks with Regards,
      Towhid
      Excel and VBA Content Developer

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo