2017-03-03 4 views
0

Sheet1의 값으로 다른 시트 (Sheet2)의 차트에 최대 배율 및 최소 배율을 업데이트하려고합니다. 그러나 개체 필요 오류가 발생합니다.Excel VBA - 차트 참조가 작동하지 않습니다.

Sub ChangeAxisScale() 
Dim wsChart As Chart 
Dim wsInput As Worksheet 
Dim LastRow As Long 


Set wsInput = ThisWorkbook.Sheets("Sheet1") 

With Sheet2.ChartObjects("Chart").Chart.Axes(xlValue) 
     LastRow = wsInput.Cells(wsInput.Rows.Count, "D").End(xlUp).Row 
     .MaximumScale = wsInput.Cells(LastRow, 4).Value 
     .MinimumScale = wsInput.Range("D2").Value 
End With 
End Sub 

라인 오류가 With Sheet2.ChartObjects("Chart").Chart.Axes(xlValue)입니다 출연 : 아래는 코드입니다. 내가 차트를 잘못 참조 했나요?

감사합니다.

답변

0

이름이 "Sheet2"인 시트에 차트가 있으면 코드가 제대로 실행됩니다. 차트의 이름이 다른 시트에 있으면 코드가 실패합니다.

Dim 문을 사용하여 차트가있는 시트를 선언하고 올바르게 설정할 수 있습니다.

Sub ChangeAxisScale() 
Dim wsChart As Chart ' you don't really need that, right? 
Dim wsInput As Worksheet, wsChartSheet As Worksheet 
Dim LastRow As Long 


Set wsInput = ThisWorkbook.Sheets("Sheet1") 
Set wsChartSheet = ThisWorkbook.Sheets("Sheet2") 

With wsChartSheet.ChartObjects("Chart").Chart.Axes(xlValue) 
     LastRow = wsInput.Cells(wsInput.Rows.Count, "D").End(xlUp).Row 
     .MaximumScale = wsInput.Cells(LastRow, 4).Value 
     .MinimumScale = wsInput.Range("D2").Value 
End With 
End Sub 
관련 문제