Chart Legende wird manchmal nicht angezeigt - Makro Rekorder

  • Excel

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von vbneuling60.

    Chart Legende wird manchmal nicht angezeigt - Makro Rekorder

    Hallo zusammen,

    ich bin gerade dabei ein VBA Tool zu schreiben. In meinem Problem geht es darum, dass ich bei der Erstellung eines Chart die Legende manchmal nicht angezeigt bekomme. Fuer die Erstellung des Charts habe ich den Makro Rekorder (2013) verwendet. Das klappt soweit wunderbar. Ich verwende ein empfohlenes Chart mit Legende. Aber wenn ich den Makro danach ausfuehre kommt es manchmal vor, dass die Legende einfach nicht angezeigt wird. Manchmal wird sie angezeigt, manchmal nicht.

    Kennt jemand dieses Problem?

    Hier ist mein Code fur die Erstellung des Charts:

    VB.NET-Quellcode

    1. Sub Macro2(sheet As String, column As Integer, exist As Integer)
    2. If exist <> 1 Then
    3. Sheets(sheet).Select
    4. Sheets.Add After:=ActiveSheet
    5. Sheets(ActiveSheet.name).Select
    6. ' Sheets(ActiveSheet.name).name = "Sheet2"
    7. Sheets(ActiveSheet.name).Select
    8. On Error Resume Next
    9. Sheets(ActiveSheet.name).name = sheet & " - Chart"
    10. On Error Resume Next
    11. End If
    12. Sheets(sheet).Select
    13. ' Range("A1:G109").Select
    14. ActiveSheet.Shapes.AddChart2(297, xlColumnStacked).Select
    15. ActiveChart.SetSourceData Source:=Sheets(sheet).Range(Cells(1, 1), Cells(109, column))
    16. ActiveChart.Parent.Cut
    17. Sheets(sheet & " - Chart").Select
    18. 'Clear all contents in sheet
    19. ActiveSheet.Cells.Clear
    20. ActiveSheet.ChartObjects.Delete
    21. ActiveSheet.Paste
    22. ActiveSheet.ChartObjects(ActiveChart.Parent.name).Activate
    23. ActiveSheet.Shapes(ActiveChart.Parent.name).IncrementLeft 48
    24. ActiveSheet.Shapes(ActiveChart.Parent.name).IncrementTop 15
    25. ActiveSheet.ChartObjects(ActiveChart.Parent.name).Activate
    26. ActiveSheet.Shapes(ActiveChart.Parent.name).ScaleWidth 3.0645833333, msoFalse, _
    27. msoScaleFromTopLeft
    28. ActiveSheet.Shapes(ActiveChart.Parent.name).ScaleHeight 1.9513888889, msoFalse, _
    29. msoScaleFromTopLeft
    30. ActiveSheet.ChartObjects(ActiveChart.Parent.name).Activate
    31. ActiveSheet.Shapes(ActiveChart.Parent.name).IncrementLeft -30
    32. ActiveSheet.ChartObjects(ActiveChart.Parent.name).Activate
    33. ActiveSheet.Shapes(ActiveChart.Parent.name).IncrementLeft 0.75
    34. ActiveSheet.Shapes(ActiveChart.Parent.name).IncrementTop -8.25
    35. ActiveSheet.ChartObjects(ActiveChart.Parent.name).Activate
    36. ActiveChart.ClearToMatchStyle
    37. ActiveChart.ChartStyle = 304
    38. Application.CommandBars("Format Object").Visible = False
    39. ActiveSheet.ChartObjects(ActiveChart.Parent.name).Activate
    40. ActiveChart.ChartTitle.Select
    41. ActiveChart.ChartTitle.Text = sheet & " - Workload"
    42. Selection.Format.TextFrame2.TextRange.Characters.Text = _
    43. sheet & " - Workload"
    44. On Error Resume Next
    45. With Selection.Format.TextFrame2.TextRange.Characters(1, 25).ParagraphFormat
    46. .TextDirection = msoTextDirectionLeftToRight
    47. .Alignment = msoAlignCenter
    48. End With
    49. On Error Resume Next
    50. With Selection.Format.TextFrame2.TextRange.Characters(1, 25).Font
    51. .BaselineOffset = 0
    52. .Bold = msoTrue
    53. .NameComplexScript = "+mn-cs"
    54. .NameFarEast = "+mn-ea"
    55. .Shadow.Type = msoShadow22
    56. .Shadow.Visible = msoTrue
    57. .Shadow.Style = msoShadowStyleOuterShadow
    58. .Shadow.Blur = 4
    59. .Shadow.OffsetX = 1.8369701987E-16
    60. .Shadow.OffsetY = 3
    61. .Shadow.RotateWithShape = msoFalse
    62. .Shadow.ForeColor.RGB = RGB(0, 0, 0)
    63. .Shadow.Transparency = 0.599999994
    64. .Shadow.Size = 100
    65. .Fill.Visible = msoTrue
    66. .Fill.ForeColor.RGB = RGB(242, 242, 242)
    67. .Fill.Transparency = 0
    68. .Fill.Solid
    69. .Size = 16
    70. .Italic = msoFalse
    71. .Kerning = 12
    72. .name = "+mn-lt"
    73. .UnderlineStyle = msoNoUnderline
    74. .Spacing = 1
    75. .Strike = msoNoStrike
    76. End With
    77. ActiveChart.ChartArea.Select
    78. Application.CommandBars("Format Object").Visible = False
    79. ActiveChart.PlotArea.Select
    80. ActiveChart.FullSeriesCollection(1).Select
    81. ActiveChart.ChartArea.Select
    82. ActiveChart.FullSeriesCollection(1).ChartType = xlLine
    83. Application.CommandBars("Format Object").Visible = False
    84. ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
    85. ' ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
    86. ActiveChart.SetElement msoElementPrimaryValueAxisTitleBelowAxis
    87. ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "hours"
    88. Selection.Format.TextFrame2.TextRange.Characters.Text = "hours"
    89. With Selection.Format.TextFrame2.TextRange.Characters(1, 5).ParagraphFormat
    90. .TextDirection = msoTextDirectionLeftToRight
    91. .Alignment = msoAlignCenter
    92. End With
    93. With Selection.Format.TextFrame2.TextRange.Characters(1, 5).Font
    94. .BaselineOffset = 0
    95. .Bold = msoTrue
    96. .Caps = msoAllCaps
    97. .NameComplexScript = "+mn-cs"
    98. .NameFarEast = "+mn-ea"
    99. .Fill.Visible = msoTrue
    100. .Fill.ForeColor.RGB = RGB(217, 217, 217)
    101. .Fill.Transparency = 0
    102. .Fill.Solid
    103. .Size = 9
    104. .Italic = msoFalse
    105. .Kerning = 12
    106. .name = "+mn-lt"
    107. .UnderlineStyle = msoNoUnderline
    108. .Strike = msoNoStrike
    109. End With
    110. ActiveChart.Axes(xlCategory).AxisTitle.Select
    111. ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "months"
    112. Selection.Format.TextFrame2.TextRange.Characters.Text = "months"
    113. With Selection.Format.TextFrame2.TextRange.Characters(1, 6).ParagraphFormat
    114. .TextDirection = msoTextDirectionLeftToRight
    115. .Alignment = msoAlignCenter
    116. End With
    117. With Selection.Format.TextFrame2.TextRange.Characters(1, 6).Font
    118. .BaselineOffset = 0
    119. .Bold = msoTrue
    120. .Caps = msoAllCaps
    121. .NameComplexScript = "+mn-cs"
    122. .NameFarEast = "+mn-ea"
    123. .Fill.Visible = msoTrue
    124. .Fill.ForeColor.RGB = RGB(217, 217, 217)
    125. .Fill.Transparency = 0
    126. .Fill.Solid
    127. .Size = 9
    128. .Italic = msoFalse
    129. .Kerning = 12
    130. .name = "+mn-lt"
    131. .UnderlineStyle = msoNoUnderline
    132. .Strike = msoNoStrike
    133. ' .SetElement (msoElementLegendRight)
    134. End With
    135. With ActiveChart.Parent
    136. .Top = Range("A1").Top
    137. .Left = Range("A1").Left
    138. 'include these lines to make it fit exactly on J22:
    139. .Height = Range("A1:A30").Height
    140. .Width = Range("A1:X1").Width
    141. End With
    142. ' ActiveChart.ChartArea.Select
    143. ' ActiveChart.SetElement (msoElementLegendRight)
    144. ' Range("X19").Select
    145. End Sub



    Mit besten Gruessen,