Run time error in VBA

Pinkoto

New Member
Joined
Oct 7, 2016
Messages
40
This code gives me an error when i open another excel file. It works great for what its supposed to do in the file it is located, but if i open another exel file (Which doesnt have any VBA) it gives me a run time error "The item with the specified name wasnt found". When i give debug it shows me this part of the code in yellow.

" Set cht = Sheets("Sheet1").ChartObjects("Chart 1").Chart"

Any suggestions?

Code:
Private Sub Worksheet_Calculate()
    Dim cht As Chart, srs As Series
    Set cht = Sheets("Sheet1").ChartObjects("Chart 1").Chart
    With Me
        cht.SetSourceData Source:=Range(.Cells(31, 2), .Cells(31 + .Range("K2").Value, 2 + .Range("L2").Value))
    End With
    For Each srs In cht.FullSeriesCollection
        With srs.Format.Line
            .ForeColor.ObjectThemeColor = msoThemeColorText1
        End With
    Next
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Replace:
Rich (BB code):
    Set cht = Sheets("Sheet1").ChartObjects("Chart 1").Chart
with
Rich (BB code):
    Set cht = ThisWorkbook.Sheets("Sheet1").ChartObjects("Chart 1").Chart
You may also need to figure out what worksheet the Range in SetSourceData should point to as it currently has no worksheet specified. Perhaps as simple as:
Rich (BB code):
cht.SetSourceData Source:=.Range(.Cells(31, 2), .Cells(31 + .Range("K2").Value, 2 + .Range("L2").Value))
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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