VBA Chart Trouble - Help with the macro

PaulExcelVBA

New Member
Joined
Mar 30, 2015
Messages
5
Hello everyone, I will explain what I need to do and what is happening, the code will be after the explanations.

What I am trying to do:

I have a small database with the average consumption of every month, it is basically two lines, one with the 12 months, along with the 12 payments.

Everytime I click my custom button, I would like it to generate a new chart containning the info OR it could be the same chart (as long as it keeps the updated info), it could be only hidden and show up when the button is pressed.

It doesn't need to be deleted, but it needs to keep the same visual pattern.

However, this chart needs to be "deleted" or hidden after it is seem.

What is happening:

After I click the button, a series of mistakes happen, it does generate the chart, but it informs the error ('-2147024809 (80070057)') and it shows the chart, but it doesn't make all the necessary visual adjustments.

I think one of the main issues is that everytime it creates a chart, it changes the name (such as chart 15, 16, 17, etc) since it is a new chart.

Code:
Sub ChartTest()
'
' ChartTest Macro
'

'
    Range("A8:L9").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Range("'Relatório CPFL'!$A$8:$L$9")
    ActiveChart.Legend.Select
    Selection.Delete
    ActiveSheet.ChartObjects("Gráfico 18").Activate
    ActiveChart.SeriesCollection(1).Select
    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorBackground2
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = -0.75
        .Transparency = 0
        .Solid
    End With
    ActiveChart.ChartArea.Select
    With ActiveSheet.Shapes("Gráfico 18").Line
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorBackground2
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = -0.25
    End With
    With ActiveSheet.Shapes("Gráfico 18").Line
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorBackground2
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = -0.8999999762
        .Transparency = 0
    End With
    ActiveSheet.Shapes("Gráfico 18").Title = ""
    ActiveChart.SetElement (msoElementChartTitleAboveChart)
    ActiveChart.ChartTitle.Text = "Consumo Mensal"
    Selection.Format.TextFrame2.TextRange.Characters.Text = "Consumo Mensal"
    With Selection.Format.TextFrame2.TextRange.Characters(1, 14).ParagraphFormat
        .TextDirection = msoTextDirectionLeftToRight
        .Alignment = msoAlignCenter
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(1, 7).Font
        .BaselineOffset = 0
        .Bold = msoTrue
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(0, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 18
        .Italic = msoFalse
        .Kerning = 12
        .Name = "+mn-lt"
        .UnderlineStyle = msoNoUnderline
        .Strike = msoNoStrike
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(8, 7).Font
        .BaselineOffset = 0
        .Bold = msoTrue
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(0, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 18
        .Italic = msoFalse
        .Kerning = 12
        .Name = "+mn-lt"
        .UnderlineStyle = msoNoUnderline
        .Strike = msoNoStrike
    End With
    ActiveChart.ChartArea.Select
End Sub

How can I fix this or proceed?

Thank you!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hello everyone, I will explain what I need to do and what is happening, the code will be after the explanations.

What I am trying to do:

I have a small database with the average consumption of every month, it is basically two lines, one with the 12 months, along with the 12 payments.

Everytime I click my custom button, I would like it to generate a new chart containning the info OR it could be the same chart (as long as it keeps the updated info), it could be only hidden and show up when the button is pressed.

It doesn't need to be deleted, but it needs to keep the same visual pattern.

However, this chart needs to be "deleted" or hidden after it is seem.

What is happening:

After I click the button, a series of mistakes happen, it does generate the chart, but it informs the error ('-2147024809 (80070057)') and it shows the chart, but it doesn't make all the necessary visual adjustments.

I think one of the main issues is that everytime it creates a chart, it changes the name (such as chart 15, 16, 17, etc) since it is a new chart.

Code:
Sub ChartTest()
'
' ChartTest Macro
'

'
    Range("A8:L9").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Range("'Relatório CPFL'!$A$8:$L$9")
    ActiveChart.Legend.Select
    Selection.Delete
    ActiveSheet.ChartObjects("Gráfico 18").Activate
    ActiveChart.SeriesCollection(1).Select
    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorBackground2
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = -0.75
        .Transparency = 0
        .Solid
    End With
    ActiveChart.ChartArea.Select
    With ActiveSheet.Shapes("Gráfico 18").Line
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorBackground2
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = -0.25
    End With
    With ActiveSheet.Shapes("Gráfico 18").Line
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorBackground2
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = -0.8999999762
        .Transparency = 0
    End With
    ActiveSheet.Shapes("Gráfico 18").Title = ""
    ActiveChart.SetElement (msoElementChartTitleAboveChart)
    ActiveChart.ChartTitle.Text = "Consumo Mensal"
    Selection.Format.TextFrame2.TextRange.Characters.Text = "Consumo Mensal"
    With Selection.Format.TextFrame2.TextRange.Characters(1, 14).ParagraphFormat
        .TextDirection = msoTextDirectionLeftToRight
        .Alignment = msoAlignCenter
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(1, 7).Font
        .BaselineOffset = 0
        .Bold = msoTrue
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(0, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 18
        .Italic = msoFalse
        .Kerning = 12
        .Name = "+mn-lt"
        .UnderlineStyle = msoNoUnderline
        .Strike = msoNoStrike
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(8, 7).Font
        .BaselineOffset = 0
        .Bold = msoTrue
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(0, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 18
        .Italic = msoFalse
        .Kerning = 12
        .Name = "+mn-lt"
        .UnderlineStyle = msoNoUnderline
        .Strike = msoNoStrike
    End With
    ActiveChart.ChartArea.Select
End Sub

How can I fix this or proceed?

Thank you!

I know that I am a newbie yet regarding VBA, but I appreciate some help with this one.
 
Upvote 0

Forum statistics

Threads
1,215,478
Messages
6,125,040
Members
449,206
Latest member
Healthydogs

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