Code for making chart sheet with varying range

mdlc805

New Member
Joined
Jun 28, 2019
Messages
3
im having trouble with making a code that will extract data out of a sheet that ill have a varying range and turning it into a chart sheet. can someone help me out please or point me in the write direction.
tried this but dont know how to vary range
Sub test1()
Dim test As Chart
Set test = Charts.Add




End Sub


also tried this but dont know how to make it to chart sheet

Sub Macro1()




ActiveSheet.Shapes.AddChart2(227, xlLine).Select
Range(Range("B2"), Range("B2").End(xlDown)).Select





End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Re: need help with code for making chart sheet with varying range

Let's assume that Column A and Column B contain the source data for the chart, and that the sheet containing the data is the active sheet, try the following macro...

Code:
Option Explicit

Sub CreateChart()


    Dim theSourceRange As Range
    Dim theChart As Chart
    Dim lastRow As Long
    
    'get the last used row in Column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    'set the source data for the chart
    Set theSourceRange = Range("A1:B" & lastRow)
    
    'create a chart sheet and place it at the beginning of the workbook
    Set theChart = Charts.Add(Before:=Sheets(1))
    
    'set some properties for the chart
    With theChart
        .ChartType = xlLine
        .SetSourceData theSourceRange
        .SetElement msoElementChartTitleAboveChart
        .ChartTitle.Text = "My Chart Title"
        .SetElement msoElementLegendBottom
    End With
    
End Sub

Hope this helps!
 
Upvote 0
Re: need help with code for making chart sheet with varying range

This is the code I wrote that seems to be working but I cant figure out how to add multiple data sets to each charts.

Sub test()
Dim test As Chart
Set test = Charts.Add
With test
test.SetSourceData Source:=Sheets("Sheet1").Range("G5").EntireColumn
.ChartType = xlLine
.HasTitle = True
.ChartTitle.Text = "test"






End With
Sheets(1).Name = "test"


Dim test1 As Chart
Set test1 = Charts.Add
With test1
test.SetSourceData Source:=Sheets("Sheet1").Range("Z5").EntireColumn
.ChartType = xlLine
.HasTitle = True
.ChartTitle.Text = "test1"




End With
Sheets(1).Name = "test1"
End Sub
 
Upvote 0
Re: need help with code for making chart sheet with varying range

Can you please clarify whether you want to create one chart or whether you want to create one or more charts? And, can you please specify where the data is located for your chart or charts?
 
Upvote 0
Re: need help with code for making chart sheet with varying range

Can you please clarify whether you want to create one chart or whether you want to create one or more charts? And, can you please specify where the data is located for your chart or charts?


I need to create multiple charts with each chart having multiple data sets. Then I need to move all charts to a new single chart sheet and it needs to be organized.
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top