Page 356 - Microsoft Office Excel 2003 Programming Inside Out
P. 356

Part 5:  Manipulating Excel Objects
                                        Microsoft Office Excel 2003 Programming Inside Out

                    Deactivating a Chart
                             When a macro is created using the recorder, you’ll find the recorder generates a statement,
                             such as

                             ActiveWindow.Visible = False
                             This statement deactivates the chart, but it’s not clear as to why the chart is deactivated.
                             When writing a macro that involves charts, try using the Deselect method.
                             ActiveChart.Deselect

                             These two statements have slightly different results. Setting the Visible property of the
                             ActivateWindow object to False causes the embedded chart to be selected but no longer
                             activated. The Deselect method will deactivate and deselect the chart.
                             Modify the AddEmbeddedChart event procedure that was created in the “Creating Embedded
                             Charts or Chart Sheets” section earlier in the chapter by setting the ActiveWindow object to
                             False to deactivate the chart.

                             Sub AddEmbeddedChart()
                                 Dim Chrt As Chart
             Chapter 15
                                 ActiveSheet.ChartObjects.Delete
                                 Set Chrt = Charts.Add
                                 Set Chrt = Chrt.Location(where:=xlLocationAsObject, Name:="Sheet1”)
                                 With Chrt
                                    .ChartType = xlColumnClustered
                                    .SetSourceData Source:=Sheets(“Sheet1”).Range(“A4:D7”), _
                                        PlotBy:=xlRows
                                    .HasTitle = True
                                    .ChartTitle.Text = “=Sheet1!R1C1"
                                    With .Parent
                                        .Top = Range(“A9”).Top
                                        .Left = Range(“A1”).Left
                                        .Name = “GSCProductChart"
                                    End With
                                 End With
                             ActiveWindow.Visible = False
                             End Sub
                             Now that you have tested the ActiveWindow method to deactivate the chart, use the deselect
                             method. Replace the ActiveWindow.Visible = False line with ActiveChart.Deselect.
                             For each procedure you create, you can evaluate which method of deactivation will achieve
                             the required results for your current scenario.









                330
   351   352   353   354   355   356   357   358   359   360   361