【问题标题】:Plot a graph with two series from a range of selected data in Excel using a macro使用宏从 Excel 中的一系列选定数据中绘制包含两个系列的图表
【发布时间】:2016-09-27 21:56:21
【问题描述】:

我在分子生物学实验室工作,并获得了一份巨大的数据表,其中包含不同条件下基因的表达水平。我想自动化绘制每个基因的不同值的过程。我对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"

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

此外,如果我可以在行的第一个单元格中添加值作为标题,那将非常有帮助,但这不是我主要关心的问题。

【问题讨论】:

标签: excel vba macros


【解决方案1】:

您的范围是静态的,因为您的 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 示例邮寄给您,请告诉我。

皮特。

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 2021-10-26
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    相关资源
    最近更新 更多