排除某些列的表格范围

时间:2018-06-22 04:14:37

标签: excel vba excel-vba

无法在vba中编码宏以将某些列排除在图表之外。我想排除F列。这是我从经过重新调整的宏中修改的代码:

Sub Macro4()

Dim ws As Worksheet
Dim wb As Workbook
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")






    ActiveSheet.Shapes.AddChart2(251, xlBarClustered).Select
    ActiveChart.SetSourceData Source:=ws.Range("E" & ActiveCell.Row & ":F" & ActiveCell.Row & ":G" & ActiveCell.Row & ":H" & ActiveCell.Row)
    ActiveChart.FullSeriesCollection(1).XValues = "=Sheet1!$E$9:$H$9"
    ActiveChart.SetElement (msoElementDataLabelOutSideEnd)
    ActiveChart.SetElement (msoElementPrimaryCategoryGridLinesNone)
    ActiveChart.Axes(xlValue).MajorGridlines.Format.Line.Visible = msoFalse
    ActiveChart.FullSeriesCollection(1).DataLabels.ShowCategoryName = False
    With ActiveChart
    .HasTitle = True
    .ChartTitle.Characters.Text = "Analysis for " & ws.Range("C" & ActiveCell.Row)
    ActiveChart.HasAxis(xlValue) = False
    ActiveChart.HasLegend = False
    End With



End Sub

2 个答案:

答案 0 :(得分:1)

这应从图表中排除F。

编辑:

要仅使用活动行中的数据创建图表,可以尝试:

Sub Macro4()

Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Sheets("Sheet1")
ws.Activate

    Dim MyRange1 As Range: Set MyRange1 = Range("E" & ActiveCell.Row)
    Dim MyRange2 As Range: Set MyRange2 = Range(Cells(ActiveCell.Row, "G"), Cells(ActiveCell.Row, "H"))
    Dim MyChartRange As Range: Set MyChartRange = Union(MyRange1, MyRange2)

    ActiveSheet.Shapes.AddChart2(251, xlBarClustered).Select
    ActiveChart.SetSourceData Source:=MyChartRange
    ActiveChart.FullSeriesCollection(1).XValues = "=Sheet1!$E$9:$H$9"
    ActiveChart.SetElement (msoElementDataLabelOutSideEnd)
    ActiveChart.SetElement (msoElementPrimaryCategoryGridLinesNone)
    ActiveChart.Axes(xlValue).MajorGridlines.Format.Line.Visible = msoFalse
    ActiveChart.FullSeriesCollection(1).DataLabels.ShowCategoryName = False

With ActiveChart
    .HasTitle = True
    .ChartTitle.Characters.Text = "Analysis for " & ws.Range("C" & ActiveCell.Row)
    ActiveChart.HasAxis(xlValue) = False
    ActiveChart.HasLegend = False
End With

End Sub

答案 1 :(得分:1)

请尝试以下操作,您使用“,”来分隔非连续范围:

ActiveChart.SetSourceData Source:=ws.Range("Sheet1!$E$" & ActiveCell.Row & ",Sheet1!$G$" & ActiveCell.Row & ",Sheet1!$H$" & ActiveCell.Row)
ActiveChart.FullSeriesCollection(1).XValues = "=Sheet1!$E$9,Sheet1!$G$9,Sheet1!$H$9"
相关问题