Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a chart from data with VBA
#1
Example:

Sub CreateChart()
    Dim chartSheet As Worksheet
    Dim dataSheet As Worksheet
    Dim chartObj As ChartObject
    Dim chartRange As Range

    'Set the data sheet where the chart data is located
    Set dataSheet = ThisWorkbook.Sheets("Sheet1")

    'Set the range of data for the chart (adjust the range as per your data)
    Set chartRange = dataSheet.Range("A1:B5")

    'Create a new worksheet to place the chart
    Set chartSheet = ThisWorkbook.Sheets.Add

    'Create the chart as a column chart on the new worksheet
    Set chartObj = chartSheet.ChartObjects.Add(Left:=50, Width:=375, Top:=75, Height:=225)
    With chartObj.Chart
        .SetSourceData Source:=chartRange
        .ChartType = xlColumnClustered
    End With
End Sub

Explanation:

The code defines a subroutine named CreateChart.
It declares variables for the chart sheet (chartSheet), data sheet (dataSheet), chart object (chartObj), and chart data range (chartRange).
Set dataSheet = ThisWorkbook.Sheets("Sheet1") specifies the worksheet ("Sheet1") where the chart data is located.
Set chartRange = dataSheet.Range("A1:B5") sets the range of data for the chart (adjust the range as per your data).
A new worksheet (chartSheet) is created to place the chart using ThisWorkbook.Sheets.Add.
The chart is created as a column chart on the new worksheet using chartSheet.ChartObjects.Add. 
The chart dimensions and position are adjusted using the Left, Width, Top, and Height parameters.
The chart source data is set to chartRange using .SetSourceData Source:=chartRange.
The chart type is set to a clustered column chart using .ChartType = xlColumnClustered.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy data to another worksheet Qomplainerz 0 398 07-27-2023, 11:24 AM
Last Post: Qomplainerz
  Create a hyperlink with VBA Qomplainerz 0 410 07-27-2023, 11:10 AM
Last Post: Qomplainerz
  Concatenate data in columns Qomplainerz 0 346 07-27-2023, 11:01 AM
Last Post: Qomplainerz
  Create a pivot table based on data in a worksheet with VBA Qomplainerz 0 414 07-27-2023, 09:48 AM
Last Post: Qomplainerz
  Filter data in a worksheet Qomplainerz 0 380 07-27-2023, 09:46 AM
Last Post: Qomplainerz
  Sort data in a range with VBA Qomplainerz 0 366 07-27-2023, 09:43 AM
Last Post: Qomplainerz
  Insert a new row and fill it with data in VBA Qomplainerz 0 343 07-27-2023, 09:11 AM
Last Post: Qomplainerz
  Add data validation to a cell with VBA Qomplainerz 0 353 07-27-2023, 09:10 AM
Last Post: Qomplainerz
  Create a new worksheet and rename it with VBA Qomplainerz 0 306 07-27-2023, 08:14 AM
Last Post: Qomplainerz
  Copy data from one workbook to another with VBA Qomplainerz 0 329 07-27-2023, 08:02 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 7 Guest(s)