0

I have the following VBA code, and when I have the workbook with this open but I go on a different workbook it comes up with an error saying "subscript out of bounds". I have added the line specifing the workbook thinking this would stop it, but it doesn't seem to be solving anything. What alterations need to be made to avoid the error?

Option Explict Private Sub Worksheet_Calculate() Dim cht As Chart Dim wks As Worksheet Dim wb As Workbook Set wb = Workbooks("The wookbook I want it to work in") Set wks = Sheets("The sheet I want it to work in") Set cht = wks.ChartObjects("Chart 1").Chart... 

1 Answer 1

1

I think you want the macro to work with the active workbook:

Set wb = ActiveWorkbook Set wks = wb.Activesheet for each cht in wks.ChartObjects .... next cht 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Patrick, this didn't quite work as it isn't the active workbook, but by doing Set wks = wb.Worksheets("the sheet") it did work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.