仅当表格中有可用数据时才生成Excel图表

时间:2018-12-27 06:22:51

标签: excel excel-vba

我在Excel工作表中有12个动态表(基于组合的表),这些工作表使用公式从其他工作表中提取数据。因此,一次一次仅一个表将具有数据,因此将仅基于该数据生成一个图。 这是我记录的图表创建宏,如果单元格具有此特定值,则会在开头添加if语句,它将创建图形,否则将不会创建图形

因此,如果组合图形表中的单元格C1中包含“ Vub”作为值,它将生成一个图形,否则将不会。

`Sub Chart_1()
If Target.Address = "$C$1" Then 
If Target.Value = "Vub" Then
Range("I2").Select
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Range("'Combination Graphs'!$A$1:$CF$1402")
ActiveChart.ChartTitle.Select
ActiveChart.ChartArea.Select
Application.CutCopyMode = False
Application.CutCopyMode = False
Application.CutCopyMode = False
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveChart.FullSeriesCollection(1).Delete
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "='Combination Graphs'!$J$1"
ActiveChart.FullSeriesCollection(1).Values = "==UAT_17.12.2018.xlsx!Vub_c2"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "='Combination Graphs'!$K$1"
ActiveChart.FullSeriesCollection(2).Values = "==UAT_17.12.2018.xlsx!Aub_c2"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(3).Name = "='Combination Graphs'!$L$1"
ActiveChart.FullSeriesCollection(3).Values = "==UAT_17.12.2018.xlsx!Vne_c2"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(4).Name = "='Combination Graphs'!$M$1"
ActiveChart.FullSeriesCollection(4).Values = "==UAT_17.12.2018.xlsx!Isig_c2"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(5).Name = "='Combination Graphs'!$N$1"
ActiveChart.FullSeriesCollection(5).Values = "==UAT_17.12.2018.xlsx!Ipe_c2"
ActiveChart.FullSeriesCollection(5).XValues = "==UAT_17.12.2018.xlsx!Date_c2"
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
ActiveChart.PlotArea.Select
ActiveChart.SetElement (msoElementLegendRight)
Application.CommandBars("Format Object").Visible = False
ActiveChart.Legend.Select
ActiveChart.Legend.Select
Selection.Position = xlBottom
Application.CommandBars("Format Object").Visible = False
ActiveChart.ChartArea.Select
ActiveSheet.Shapes("Chart 7").ScaleWidth 0.9563952318, msoFalse, _
    msoScaleFromTopLeft
ActiveSheet.Shapes("Chart 7").ScaleHeight 1.1266149023, msoFalse, _
    msoScaleFromTopLeft
Range("O26").Select
ActiveSheet.ChartObjects("Chart 7").Activate
ActiveChart.Axes(xlCategory).Select
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "Steady State Occurence 2"
Selection.Format.TextFrame2.TextRange.Characters.Text = _
    "Steady State Occurence 2"
With Selection.Format.TextFrame2.TextRange.Characters(1, 24).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 12).Font
    .BaselineOffset = 0
    .Bold = msoFalse
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(89, 89, 89)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 14
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Spacing = 0
    .Strike = msoNoStrike
End With
With Selection.Format.TextFrame2.TextRange.Characters(13, 12).Font
    .BaselineOffset = 0
    .Bold = msoFalse
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(89, 89, 89)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 14
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Spacing = 0
    .Strike = msoNoStrike
End With
ActiveChart.ChartArea.Select
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Magnitude"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Magnitude"
With Selection.Format.TextFrame2.TextRange.Characters(1, 9).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 9).Font
    .BaselineOffset = 0
    .Bold = msoFalse
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(89, 89, 89)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 10
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Strike = msoNoStrike
End With
ActiveChart.Axes(xlCategory).AxisTitle.Select
Selection.Left = 166.053
Selection.Top = 195.268
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Date/Time"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Date/Time"
With Selection.Format.TextFrame2.TextRange.Characters(1, 9).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 9).Font
    .BaselineOffset = 0
    .Bold = msoFalse
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(89, 89, 89)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 10
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Strike = msoNoStrike
End With
End if
End if
ActiveChart.ChartArea.Select
End Sub`

This is the output i need

2 个答案:

答案 0 :(得分:0)

下面的代码检查“ Sheet1”中名为“ tblTest”的表是否为空。更改表和工作表名称并执行以下代码:

Option Explicit

 Sub Rows_Counts()

    Dim tbl As ListObject

    Set tbl = Sheet1.ListObjects("tblTest")

    With tbl

        If .DataBodyRange Is Nothing Then

            MsgBox "Empty Table"

        End If

        Set tbl = Nothing

    End With

 End Sub

答案 1 :(得分:0)

此代码应指导您正确的方向(适用于Excel 2013 +):

<html amp lang="tr">
<head>
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
</head>
<body>
<form method="post" action-xhr="#" autocomplete="off">
    <?PHP echo '<p>' . $yorumcevap . '</p>'; ?>
    <input type="text" class="isim" name="isim" autofocus required>
    <input type="text" class="isim" name="konu" required>
    <input type="text" class="isim" name="email" required>
    <textarea name="mesaj" cols="80" rows="8" required></textarea>
    <input type="submit" value="Gönder" name="iletisimg">
</form>
</body>
</html>