首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用宏从Excel中的选定数据范围绘制包含两个系列的图形

使用宏从Excel中的选定数据范围绘制包含两个系列的图形
EN

Stack Overflow用户
提问于 2016-09-28 05:56:22
回答 1查看 1K关注 0票数 0

我在一家分子生物学实验室工作,获得了一份包含不同条件下基因表达水平的巨大数据表。我想自动化绘制每个基因的不同值的过程。我对VBA一无所知,只能大致理解“记录宏”函数的输出。

我的数据排列如下(在一行中):基因名称,condition1中的表达,condition2...直到条件6。我想要做的是:我想选择一行,在完成宏之后,我想在一个系列中有一个带有条件1-3的图,在第二个系列中有一个条件为4-6的图。

当我选择第一行并录制一个宏时,我会得到这样的结果:

代码语言:javascript
运行
复制
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

这对第一行有效,但是当我在其他数据行上运行它时,我得到了第一个图,因为值的来源是固定的。我想要更改的是

代码语言:javascript
运行
复制
ActiveChart.FullSeriesCollection(1).Values = "=Sheet1!$B$4:$D$4"

设置为选定范围内的第二到第四个值。中的值

代码语言:javascript
运行
复制
ActiveChart.FullSeriesCollection(2).Values = "=Sheet1!$E$4:$G$4"

设置为选定行中的第五到第七个值。

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

EN

回答 1

Stack Overflow用户

发布于 2016-09-28 08:19:24

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

但是,使用命名范围和数据透视表有一种更动态的方法来完成此操作。当在动态范围中添加或删除记录时,动态范围将自动缩小和增大。定义一个新的范围(公式/定义名称),并将“引用”行设置为类似于"=OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),7)“

然后创建一个数据透视表,将此范围作为其数据源。然后创建数据透视表图表。手动创建这些-但您只需手动创建一次。

然后,您需要在工作表双击事件上输入一些代码(用鼠标右键单击工作表选项卡,然后选择“查看代码”,输入以下代码:

代码语言:javascript
运行
复制
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

当您在表上双击A列中的单元格时,双击的透视值将转移到透视表筛选器字段并更改图形。图表标题也将更改,以显示您双击的值。

如果你想让我把excel的例子寄给你,请告诉我。

皮特。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39734957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档