vba - Excel 宏图删除空白图例键

标签 vba excel excel-2010

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

我需要 Sub FixPlott 方面的帮助。我试图让它删除图例键上的图例条目。例如,如果我选择主校区和南厅,则会有邓布兰和格林斯堡的空白图例条目。我希望图例只是为了显示选定的建筑物。

最佳答案

这里有您的子程序的更正版本:

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

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

关于vba - Excel 宏图删除空白图例键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17350004/

相关文章:

vba - 如何确定包含各种形状的工作表上的形状类型?

vba - 如何检索长度超过 255 个字符的 range.address?

c++ - 解析csv文件c++

excel - 将纯文本粘贴到excel中,缺少空白列?

excel-2010 - 如何在 excel 2010 中执行此操作,函数数组的总和?

vba - 从另一个模块调用私有(private)子模块

excel - 公式中 Range.FormulaLocal 和文本字符串的问题

excel - 通过匹配范围中的字符串来过滤数据

c# - 解析前过滤LinqToExcel中的空白行(转换/映射)

excel - 如何在 MS Excel 中使用索引匹配公式求和