Excel VBA는 이벤트를 사용하여 차트를 자동으로 업데이트합니다.

1196 단어 vba
Private Sub Workbook_SheetActivate(ByVal sh As Object)
Dim shrt As Worksheet
Dim rg As Range
Dim rgChartData As Range
Dim chrt As Chart
On Error GoTo NO
'Application.ScreenUpdating = False
Set shrt = sh
'Application.StatusBar = shrt.Name & " ..."
'MsgBox (sh.Name)
Set rg = shrt.Range("a2")
Do Until IsEmpty(rg)
    Set rg = rg.Offset(0, 1)
Loop
    Set rg = rg.Offset(0, -1)
    Set rgChartData = shrt.Range("a1: " & rg.Address & ",a4:" & rg.Offset(2, 0).Address)
       
If shrt.Name = ThisWorkbook.Sheets(2).Name Then
    Set chrt = shrt.ChartObjects(1).Chart
Else
    Set chrt = shrt.ChartObjects(2).Chart
End If
With chrt
    .SetSourceData rgChartData
    .ChartTitle.Text = Left(rgChartData.Cells(1, 1).Value, Len(rgChartData.Cells(1, 1).Value) - 1) & " "
End With
If shrt.Name <> ThisWorkbook.Sheets(2).Name Then
    Set chrt = shrt.ChartObjects(1).Chart
    With chrt
        .Axes(xlCategory).MaximumScale = rg.Column
    End With
End If
'Application.ScreenUpdating = True
Exit Sub
NO: 'Application.StatusBar = shrt.Name & " !CodeName=" & shrt.CodeName
End Sub

좋은 웹페이지 즐겨찾기