Excel 2010 VBA works in all open files

I have one excel file (* .xlsm) with VBA code on the first sheet:

Private Sub Worksheet_Calculate()
ActiveSheet.ChartObjects("Podtlak").Chart.Axes(xlCategory, xlPrimary).MaximumScale = Range("AV79").Value
End Sub

And the second excel file with a macro that changes the value in the cell in the first excel (it is automatically recalculated), and then copies the value of the new result from the first excel and pastes it into the second excel file.

The problem is this: when the macro goes into the second excel and paste value, the worksheet is recounted, and the code from the first excel calls, but it stops with an error, because in the second excel it cannot find the Podtlak chart object.

How to set workheet_calculate () to run only for whitch one file written?

+3
source share
1 answer

Try to specify a book:

ThisWorkbook.ActiveSheet.ChartObjects("Podtlak").Chart.Axes(xlCategory, xlPrimary).MaximumScale = Range("AV79").Value

:

ThisWorkbook.Worksheets("yourWorksheetName").ChartObjects("Podtlak").Chart.Axes(xlCategory, xlPrimary).MaximumScale = Range("AV79").Value

, , . , ActiveSheet ActiveWorkbook, , , .

+3

All Articles