2

I am having a problem here with my VBA code.

This is the first code I am writing in VBA. Basically I want it to check a condition (Value of cell F7 in sheet2), and based on that I would like to paste one of two charts in sheet2.

    Private Sub CommandButton1_Click()


CommandButton1.Caption = "Stock Size Range"
CommandButton1.BackColor = 0
CommandButton1.ForeColor = 16777215

'Clear the chart area
 Charts("Chart41").ChartArea.Clear


If Sheets("sheet2").Range("F7") = 1 Then 'Aluminum Material


    Sheets("sheet3").ChartObjects("Chart666").Select
    Sheets("sheet3").ChartObjects("Chart666").Copy
    ChartObjects("Chart41").Paste

    Else

    Sheets("sheet4").ChartObjects("Chart888").Select
    Sheets("sheet4").ChartObjects("Chart888").Copy
    ChartObjects("Chart41").Paste

End If

End Sub

Thing is,, when I click on the command button, it gives me a Run-Time error "9": Subscript out of range and it points to the line ( Charts("Chart41").ChartArea.Clear)

Thank you,

6
  • ChartObjects("Chart41").Chart.ChartArea.Clear Commented Aug 4, 2016 at 16:57
  • Thanks, it says the item with the specified name was not found,, I am pretty sure I changed the name.. What do you think? Commented Aug 4, 2016 at 17:02
  • @TimWilliams I managed to make it work. Can you please tell me why my original code did not work? Commented Aug 4, 2016 at 17:23
  • A Chart object can either be a Chart sheet, or if it's hosted on a regular worksheet it will be contained in a ChartObject - on a worksheet it's the ChartObject which has a name, not the Chart. Commented Aug 4, 2016 at 17:46
  • One more questions, apparently the line [ Sheets("sheet1").ChartObjects("chart41").Chart.ChartArea.Clear] ends up deleting the whole chart,, so then when the code wants to paste another chart on Chart41, it can not find it because that line deleted it away!! @TimWilliams Commented Aug 4, 2016 at 18:04

1 Answer 1

3

(Sorry but I do not have enough reputation to write a comment and ask about more information, so I will do my best!)

Hello!

Maybe you need to go to the desired sheet where the chart will be pasted, create a chart and then associate an ID to it, for example "MyNewChart" (you change the chart ID in the "area" where is written "Node 5" in a red rectangle in this image https://i.sstatic.net/hf2Nq.png)

Then in your code, when you need to paste the chart, you can write:

Sheets("WriteSheetNameHere").ChartObjects("MyNewChart").Paste

HTH ;)

2
  • The code is not working again,, can you please advise? @RCaetano Commented Aug 4, 2016 at 17:40
  • What is the behaviour? Only works the first time or it is another thing? Maybe the best approach would be deleting the existing chart (your chart41), then creating a new one before pasting, assign an id to it and finally paste the chart. This steps should be done each time you needed to execute the macro
    – RCaetano
    Commented Aug 5, 2016 at 8:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.