+ Reply to Thread
Results 1 to 10 of 10

Deactivate chart created with VBA

Hybrid View

kcaprik Deactivate chart created with... 06-17-2010, 10:06 AM
romperstomper Re: Deactivate chart created... 06-17-2010, 11:03 AM
kcaprik Re: Deactivate chart created... 06-17-2010, 12:22 PM
romperstomper Re: Deactivate chart created... 06-17-2010, 12:47 PM
kcaprik Re: Deactivate chart created... 06-17-2010, 12:53 PM
romperstomper Re: Deactivate chart created... 06-17-2010, 05:17 PM
teylyn Re: Deactivate chart created... 06-17-2010, 09:38 PM
kcaprik Re: Deactivate chart created... 06-18-2010, 09:37 AM
romperstomper Re: Deactivate chart created... 06-18-2010, 12:11 PM
harish.bisht Re: Deactivate chart created... 06-03-2017, 03:52 PM
  1. #1
    Registered User
    Join Date
    06-17-2010
    Location
    Texas
    MS-Off Ver
    Excel 2000, Excel 2003, Excel 2007
    Posts
    4

    Deactivate chart created with VBA

    I've programmically created a chart on the fly in Excel 2007. I'm running into an issue with the chart "deselecting" after being created. If I step through my code it all works with no issue, however if I let it run on its own the range selection at the end does not actually happen. Any ideas?

    Sub FormatChart_Coverage(strChart As String)
    
    Dim srs As series
    
        ThisWorkbook.Sheets("Report").ChartObjects(strChart).Activate
        
            ActiveChart.SetElement (msoElementLegendNone) ''Hide the legend
            ActiveChart.SetElement (msoElementDataTableWithLegendKeys) ''Show the data table
        
        For Each srs In ActiveChart.SeriesCollection
        
            If srs.Name = "Total" Then
                srs.AxisGroup = 2
                srs.Select
                With Selection.Border
                    .Weight = xlThin
                    .LineStyle = xlNone
                End With
                Selection.Interior.ColorIndex = xlNone
    
            ElseIf srs.Name = "YTD Goal" Then
                srs.ChartType = xlLineMarkers
                srs.Select
                With Selection
                    .MarkerStyle = -4115
                    .MarkerSize = 13
                    .MarkerBackgroundColorIndex = 3
                    .MarkerForegroundColorIndex = 3
                End With
                With Selection.Border
                    .LineStyle = xlNone
                End With
            End If
        Next
        
         ActiveChart.DataTable.Select
            Selection.Font.Size = 8
        
        ActiveChart.Axes(xlValue, xlSecondary).Select
            Selection.MajorTickMark = xlNone
            Selection.TickLabelPosition = xlNone
    
        ThisWorkbook.Sheets("Report").ChartObjects(strChart).Select
            Selection.OnAction = "'" & ThisWorkbook.Name & "'!Drill_CoverageForm"
            
        ActiveChart.Deselect
        
        ActiveSheet.Range("A1").Select
    
    End Sub
    Last edited by kcaprik; 06-18-2010 at 09:33 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Deactivate chart created with VBA

    You shouldn't need to select it in the first place:
    
    Sub FormatChart_Coverage(strChart As String)
        Dim cht As Chart
        Dim srs As Series
        
        Set cht = ThisWorkbook.Sheets("Report").ChartObjects(strChart).Chart
        With cht
            .SetElement (msoElementLegendNone) ''Hide the legend
            .SetElement (msoElementDataTableWithLegendKeys) ''Show the data table
        
            For Each srs In .SeriesCollection
            
                If srs.Name = "Total" Then
                    srs.AxisGroup = 2
                    With srs.Border
                        .Weight = xlThin
                        .LineStyle = xlNone
                    End With
                    srs.Interior.ColorIndex = xlNone
                    
                ElseIf srs.Name = "YTD Goal" Then
                    srs.ChartType = xlLineMarkers
                    With srs
                        .MarkerStyle = -4115
                        .MarkerSize = 13
                        .MarkerBackgroundColorIndex = 3
                        .MarkerForegroundColorIndex = 3
                    End With
                    srs.Border.LineStyle = xlNone
                End If
            Next srs
        
            .DataTable.Font.Size = 8
        
            With .Axes(xlValue, xlSecondary)
                .MajorTickMark = xlNone
                .TickLabelPosition = xlNone
            End With
        End With
        
        ThisWorkbook.Sheets("Report").ChartObjects(strChart).OnAction = "'" & ThisWorkbook.Name & "'!Drill_CoverageForm"
    
    End Sub
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    06-17-2010
    Location
    Texas
    MS-Off Ver
    Excel 2000, Excel 2003, Excel 2007
    Posts
    4

    Re: Deactivate chart created with VBA

    I was able to assign the action without selecting the chart, however I do have to have the chart active in order to change the series properties. After which the chart does not deactivate, unless I step through the code line by line.

    Updated code:

    Sub FormatChart_Coverage(strChart As String)
    
    Dim srs As series
    
        ThisWorkbook.Sheets("Report").ChartObjects(strChart).Activate
        
            ActiveChart.SetElement (msoElementLegendNone) ''Hide the legend
            ActiveChart.SetElement (msoElementDataTableWithLegendKeys) ''Show the data table
        
        For Each srs In ActiveChart.SeriesCollection
        
            If srs.Name = "Total" Then
                srs.AxisGroup = 2
                srs.Select
                With Selection.Border
                    .Weight = xlThin
                    .LineStyle = xlNone
                End With
                Selection.Interior.ColorIndex = xlNone
    
            ElseIf srs.Name = "YTD Goal" Then
                srs.ChartType = xlLineMarkers
                srs.Select
                With Selection
                    .MarkerStyle = -4115
                    .MarkerSize = 13
                    .MarkerBackgroundColorIndex = 3
                    .MarkerForegroundColorIndex = 3
                End With
                With Selection.Border
                    .LineStyle = xlNone
                End With
            End If
        Next
              
        ActiveChart.DataTable.Font.Size = 8
        
        ActiveChart.Axes(xlValue, xlSecondary).MajorTickMark = xlNone
        ActiveChart.Axes(xlValue, xlSecondary).TickLabelPosition = xlNone
    
        ThisWorkbook.Sheets("Report").ChartObjects(strChart).OnAction = "'" & ThisWorkbook.Name & "'!Drill_CoverageForm"
             
        ThisWorkbook.Sheets("Report").Activate
        ActiveSheet.Range("A1").Select
    
    End Sub
    Last edited by kcaprik; 06-18-2010 at 09:33 AM.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Deactivate chart created with VBA

    Quote Originally Posted by kcaprik View Post
    I was able to assign the action without selecting the chart, however I do have to have the chart active in order to change the series properties.
    why? Did you try my code at all?

    Also, can you please put code tags round your code, per the forum rules?

  5. #5
    Registered User
    Join Date
    06-17-2010
    Location
    Texas
    MS-Off Ver
    Excel 2000, Excel 2003, Excel 2007
    Posts
    4

    Re: Deactivate chart created with VBA

    I missed the "with cht" portion earlier. But, even after updating to your code, I'm still having the same issue.

    How do I do code tags?

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Deactivate chart created with VBA

    you type:
    [code]your code here[/code]

    I don't see how you could have the same issue with my code since it does not select the chart at any point.

  7. #7
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Deactivate chart created with VBA

    kcaprik, please take a moment to look at the forum rules and then edit your posts to add code tags.

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

  8. #8
    Registered User
    Join Date
    06-17-2010
    Location
    Texas
    MS-Off Ver
    Excel 2000, Excel 2003, Excel 2007
    Posts
    4

    Re: Deactivate chart created with VBA

    I've added code tags.

    When the code starts editing the series it activates the chart.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Deactivate chart created with VBA

    I'm not sure what you are running, because I've tested it and it does not activate the chart and nor would I expect it to. Can you post the exact code you are using from start to finish?

  10. #10
    Registered User
    Join Date
    12-29-2011
    Location
    Muimbai
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Deactivate chart created with VBA

    I'm agreed with romperstomper. A good programmer never select object in coding you need to set it.
    this is my ways to work out.

    Set ch = Sheet4.ChartObjects("Chart 2")
    Set chc = ch.Chart.SeriesCollection
    For Each chs In chc
    chs.Delete
    Next
    ch.Chart.SeriesCollection.Add Source:=Sheet2.Range(Sheet2.Cells(9 + i, S.Value + 9).Address & ":" & Sheet2.Cells(9 + i, E.Value + 9).Address), Rowcol:=xlRows, SeriesLabels:=True

    These are few examples

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1