vba Excel 宏图删除空白图例键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17350004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:55:09  来源:igfitidea点击:

Excel Macro Graph Removing Blank Legend Keys

excelvbaexcel-vbaexcel-2010

提问by Michael Downey

Option Explicit

Public PlotName As String
Public PlotRange As Range

Sub Tester()
Range("TCKWH.V.1").Select
AddPlot ActiveSheet.Range("KWH_G_1")
End Sub


Sub AddPlot(rng As Range)
With ActiveSheet.Shapes.AddChart
PlotName = .Name
.Chart.ChartType = xlLineMarkers
.Chart.SetSourceData Source:=Range(rng.Address())
.Chart.HasTitle = True
.Chart.ChartTitle.Text = Range("KWH.G.1")
.Chart.Axes(xlValue, xlPrimary).HasTitle = True
 .Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = Range("KWH.G.1")
 End With
Set PlotRange = rng
Application.EnableEvents = False
rng.Select
Application.EnableEvents = True
End Sub


Sub FixPlott(rng As Range)
Dim n As Long
With ActiveSheet.Shapes(PlotName)
  For n = .SeriesCollection.Count To 1 Step -1
  With .SeriesCollection(n)
      If PlotName = "" Then
          .Delete
        End If
      End With
      Next n
    End With
    End Sub
Sub RemovePlot(rng As Range)
 If Not PlotRange Is Nothing Then
   If Application.Intersect(rng, PlotRange) Is Nothing Then
       On Error Resume Next
        rng.Parent.Shapes(PlotName).Delete
        On Error GoTo 0
   End If
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   Application.ScreenUpdating = False
 RemovePlot Target
      Application.ScreenUpdating = True
End Sub

enter image description here

在此处输入图片说明

I need help with Sub FixPlott. I am trying to get it to delete the Legend Entries on the Legend Key. For example if I select Main Campus and South Hall there will be blank legend entries for dunblane and greensburg. Id like the legend just to show selected buildings.

我需要关于 Sub FixPlott 的帮助。我试图让它删除图例键上的图例条目。例如,如果我选择 Main Campus 和 South Hall,则 dunblane 和 greensburg 将出现空白图例条目。我喜欢图例只是为了显示选定的建筑物。

采纳答案by varocarbas

Here you have a corrected version of your sub:

在这里,您有一个更正的子版本:

Sub FixPlott(PlotName As String)
   Dim n As Long
   With ActiveSheet.Shapes(PlotName).Chart
     For n = .SeriesCollection.Count To 1 Step -1
        With .SeriesCollection(n)
            If .Name = "" Then
               ActiveSheet.Shapes(PlotName).Chart.Legend.LegendEntries(n).Delete
            End If
        End With
     Next n
   End With
End Sub

I am not sure about the exact trigger you want to use. So I have included a simple string trigger; if the given SeriesCollection is called like trigger, the legend will be deleted.

我不确定您要使用的确切触发器。所以我包含了一个简单的字符串trigger;如果给定的 SeriesCollection 被调用trigger,则图例将被删除。