0% found this document useful (0 votes)
87 views

VBA Code To Arrange The Charts

This VBA code contains two macros. The first macro arranges charts on a worksheet into a grid with three columns. It sets the width, height, and position of each chart object. The second macro formats a specific chart by setting the minimum and maximum values for the x- and y-axes, adjusting the bar gap width, and formatting the font properties of the chart text.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

VBA Code To Arrange The Charts

This VBA code contains two macros. The first macro arranges charts on a worksheet into a grid with three columns. It sets the width, height, and position of each chart object. The second macro formats a specific chart by setting the minimum and maximum values for the x- and y-axes, adjusting the bar gap width, and formatting the font properties of the chart text.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

VBA code to arrange the charts

Sub CreateGridOfCharts()

Dim int_cols As Integer


int_cols = 3

Dim cht_width As Double


cht_width = 250

Dim cht_height As Double


cht_height = 200

Dim offset_vertical As Double


offset_vertical = 195

Dim offset_horz As Double


offset_horz = 40

Dim sht As Worksheet


Set sht = ActiveSheet

Dim count As Integer count = 0


'iterate through ChartObjects on current sheet

Dim cht_obj As ChartObject


For Each cht_obj In sht.ChartObjects
'use integer division and Mod to get position in grid
cht_obj.Top = (count / int_cols) * cht_height + offset_vertical
cht_obj.Left = (count Mod int_cols) * cht_width + offset_horz
cht_obj.Width = cht_width
cht_obj.Height = cht_height
count = count + 1
Next cht_obj

End Sub
VBA code for chart formatti ng (for constant width of bars)

‘https://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-graph
‘https://stackoverflow.com/questions/36570528/how-to-specify-fixed-width-to-chart-bars
‘For horizontal bar charts, fix the y-scale minimum and maximum
‘For vertical bar charts, fix the x-scale minimum and maximum

Sub ChangeChartFormatting()

Dim cht As Chart

Set cht = ActiveSheet.ChartObjects("Chart 1").Chart

'Adjust y-axis Scale


  cht.Axes(xlValue).MinimumScale = 40
  cht.Axes(xlValue).MaximumScale = 100

'Adjust x-axis Scale


  cht.Axes(xlCategory).MinimumScale = 1
  cht.Axes(xlCategory).MaximumScale = 10
  
'Adjust Bar Gap
  cht.ChartGroups(1).GapWidth = 60

'Format Font Size


  cht.ChartArea.Format.TextFrame2.TextRange.Font.Size = 12
  
'Format Font Type
  cht.ChartArea.Format.TextFrame2.TextRange.Font.Name = "Arial"
  
'Make Font Bold
  cht.ChartArea.Format.TextFrame2.TextRange.Font.Bold = msoTrue
  
'Make Font Italicized
  cht.ChartArea.Format.TextFrame2.TextRange.Font.Italic = msoTrue

End Sub

You might also like