dave3009

CreateChartInAnotherSheet

Nov 10th, 2025
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.53 KB | Source Code | 0 0
  1. Sub CreateChartInAnotherSheet()
  2.     Dim wsAP As Worksheet, wsAR As Worksheet, wsDash As Worksheet
  3.     Dim co As ChartObject
  4.     Dim cht As Chart
  5.  
  6.     ' Sheets
  7.     Set wsAP = ThisWorkbook.Sheets("AP")              ' Expenditure
  8.     Set wsAR = ThisWorkbook.Sheets("AR Invoice")      ' Revenue
  9.     Set wsDash = ThisWorkbook.Sheets("exec dasboard") ' Target (check spelling)
  10.  
  11.     ' Optional: replace existing chart with the same name
  12.     On Error Resume Next
  13.     wsDash.ChartObjects("RevVsExp").Delete
  14.     On Error GoTo 0
  15.  
  16.     ' Create chart ON the dashboard
  17.     Set co = wsDash.ChartObjects.Add(Left:=30, Top:=30, Width:=500, Height:=300)
  18.     co.Name = "RevVsExp"
  19.     Set cht = co.Chart
  20.  
  21.     With cht
  22.         .ChartType = xlLine
  23.         .HasLegend = True
  24.         .SetElement msoElementLegendRight
  25.  
  26.         ' Expenditure
  27.         With .SeriesCollection.NewSeries
  28.             .Name = "Expenditure"
  29.             .Values = wsAP.Range("I2:I51")
  30.             .XValues = wsAP.Range("E2:E51")
  31.         End With
  32.  
  33.         ' Revenue
  34.         With .SeriesCollection.NewSeries
  35.             .Name = "Revenue"
  36.             .Values = wsAR.Range("I2:I51")
  37.             .XValues = wsAR.Range("E2:E51")
  38.         End With
  39.  
  40.         .HasTitle = True
  41.         .ChartTitle.Text = "Revenue vs Expenditure"
  42.  
  43.         With .Axes(xlCategory)
  44.             .HasTitle = True
  45.             .AxisTitle.Text = "Months"
  46.         End With
  47.         With .Axes(xlValue)
  48.             .HasTitle = True
  49.             .AxisTitle.Text = "Php"
  50.         End With
  51.     End With
  52. End Sub
  53.  
Advertisement
Add Comment
Please, Sign In to add comment