Chart Problems

CrispyAsian

Board Regular
Joined
Sep 22, 2017
Messages
64
Hello all,

I'm having trouble with a chart that is being built within my macro. It builds the chart just fine, but the two sets of data I want on the chart are 2 different columns and when the macro is done running instead of B25:B(LastRow) and F25:F(LastRow) being selected it has B25:B(LastRow) and C25:F(LastRow). Can someone tell me what I'm doing wrong?


Code:
Dim myWorksheet As Worksheet
Dim mySourceData As Range
Dim myChart As Chart
Dim myChartDestination As Range
Dim rng1 As Range, rng2 As Range
Dim Lastrow As Long
    
Lastrow = Range("A" & Rows.Count).End(xlUp).Row + 1



Set myWorksheet = ThisWorkbook.Worksheets("Overall Percentages")


Set rng1 = Range("B25:B" & Lastrow - 1)
Set rng2 = Range("F25:F" & Lastrow - 1)


With myWorksheet
    Set mySourceData = .Range(rng1, rng2)
    Set myChartDestination = .Range("A2:L23")
    Set myChart = .Shapes.AddChart(XlChartType:=xlColumnClustered, Left:=myChartDestination.Cells(1).Left, Top:=myChartDestination.Cells(1).Top, Width:=myChartDestination.Width, Height:=myChartDestination.Height).Chart
End With
    
myChart.SetSourceData Source:=mySourceData

P.S. I have to have the LastRow be +1 for other parts of the macro, that's why they have a -1 just to avoid confusion.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Give this a try:


Code:
Dim myWorksheet As Worksheet
Dim mySourceData As Range
Dim myChart As Chart
Dim myChartDestination As Range
Dim rng1 As Range, rng2 As Range
Dim Lastrow As Long
    
Lastrow = Range("A" & Rows.Count).End(xlUp).Row + 1






Set myWorksheet = ThisWorkbook.Worksheets("Overall Percentages")






Set rng1 = Range("B25:B" & Lastrow - 1)
Set rng2 = Range("F25:F" & Lastrow - 1)


With myWorksheet




    Set myChartDestination = .Range("A2:L23")
    Set myChart = .Shapes.AddChart(XlChartType:=xlColumnClustered, Left:=myChartDestination.Cells(1).Left, Top:=myChartDestination.Cells(1).Top, Width:=myChartDestination.Width, Height:=myChartDestination.Height).Chart


    myChart.SetSourceData Source:=.Range(rng1.Address & "," & rng2.Address)
    
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,614
Members
452,661
Latest member
Nonhle

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