使用宏在Excel中从一系列选定数据中绘制两个系列的图形

时间:2016-09-27 21:56:22

标签: excel vba macros

我在分子生物学实验室工作,并获得了一个巨大的数据表,其中包含不同条件下基因的表达水平。我想自动化绘制每个基因的不同值的过程。我不知道VBA,只能粗略地理解"记录宏的输出"功能

我的数据安排如下(在一行中):基因名称,条件在条件1,条件2 ......直到条件6.我想要做的是:我想选择一行,在做完宏之后,我希望有一个情节,条件1-3在一个系列中,4-6在第二个系列中。

当我选择第一行并录制宏时,我得到了这个:

Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Range("Sheet1!$A$4:$G$4")
    ActiveChart.FullSeriesCollection(1).Delete
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.FullSeriesCollection(1).Name = "=Sheet1!$B$3"
    ActiveChart.FullSeriesCollection(1).Values = "=Sheet1!$B$4:$D$4"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.FullSeriesCollection(2).Name = "=Sheet1!$E$3"
    ActiveChart.FullSeriesCollection(2).Values = "=Sheet1!$E$4:$G$4"
    ActiveChart.FullSeriesCollection(2).XValues = "=Sheet1!$B$2:$D$2"
    ActiveChart.SetElement (msoElementChartTitleAboveChart)

End Sub

这适用于第一行,但是当我在其他数据行上运行时,我得到第一个图,因为值的来源是固定的。我想改变的是

中的固定值
ActiveChart.FullSeriesCollection(1).Values = "=Sheet1!$B$4:$D$4"

到我所选范围内的第二到第四个值。

中的值
ActiveChart.FullSeriesCollection(2).Values = "=Sheet1!$E$4:$G$4"

到我所选行中的第五到第七个值。

另外,如果我可以在行的第一个单元格中添加标题作为标题,那将非常有用,但这不是我的主要关注点。

1 个答案:

答案 0 :(得分:0)

您的范围是静态的,因为您的R1C1引用前面有美元符号。尝试从您的代码中删除美元符号。

然而,使用命名范围和数据透视表有一种更动态的方法。当记录从范围中添加或删除时,动态范围将自动缩小和增长。定义一个新范围(公式/定义名称)并设置"指的是"行阅读" = OFFSET(Sheet1!$ A $ 1,0,0,COUNTA(Sheet1!$ A:$ A),7)"

然后创建一个以此范围为数据源的数据透视表。然后创建一个数据透视表。手动创建这些 - 但您只需手动创建一次。

然后,您需要在工作表双击事件中输入一些代码(右键单击工作表选项卡并选择'查看代码'输入以下代码:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

'Double-click cell to update chart
'You need to create a dynamic range and refer to that range as the pivottable data source
'For this to work you need to create a pivot table and a pivot table chart

If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub 'code will only run if values in col A are double clicked

On Error GoTo ErrorHandler

Dim Pt As PivotTable                                        'refer to pivottables as shorter text 'Pt'
Dim Fd As PivotField                                        'refer to pivottable fields as shorter text 'Fd'
Dim FilterChoice As String                                  'declare that a text string will be used as the filter choice

Set Pt = ActiveSheet.PivotTables("PivotTable3")         'tell this code where the pivottable is and what its called
Set Fd = Pt.PivotFields("Gene Name")                    'tell this code what pivottable field you are changing
FilterChoice = CStr(ActiveCell)                         'tell this code that the pivottable filter is the text in the cell you double-click

    With Pt                                             'these lines refresh the pivottable after you have double clicked
    Fd.ClearAllFilters
    Fd.CurrentPage = FilterChoice
    Pt.RefreshTable
    End With

ActiveSheet.ChartObjects("Gene_Chart").Activate             'Sets the chart title. Name the chart as 'Gene_Chart' first
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.charttitle.Text = CStr(ActiveCell)

ErrorExit:
Exit Sub

ErrorHandler:
    msgbox "Dont click on an empty cell"

ActiveSheet.ChartObjects("Gene_Chart").Activate             'Sets the chart title. Name the chart as 'Gene_Chart' first
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.charttitle.Text = "All"

Resume ErrorExit

End Sub

双击桌面上的col A中的单元格时,双击的轴心值将转移到数据透视表过滤器字段并更改图形。图表标题也会更改以显示您双击的值。

如果您希望我将excel示例邮寄给您,请告诉我。

皮特。