用于散点图绘制的动态多/单活动表参考

时间:2013-08-22 23:55:00

标签: vba

我正在尝试编写一个宏,允许我选择一些要在散点图上绘制的单元格。

但是目前我只能硬编码工作表名称,从中选择用于制图的单元格条目。

我希望能够更改硬编码以允许动态表格参考。

这应该允许从不同的工作表中选择多个单元格,然后将它们的值绘制到散点图中。

我附上以下代码供您参考。 TQ 现在关于硬编码表名称的问题:  .Name =“='US Sector(2)'!” &安培; MultiSel.Address(ReferenceStyle:= xlA1) 希望改变  .Name =“= [动态表格参考]!” &安培; MultiSel.Address(ReferenceStyle:= xlA1)

Sub ChartUSA()
' Keyboard Shortcut: Ctrl+y
'
    Dim MultiSel As Range
    Dim addr, cellAdd_name, cellAdd_Xaxis, cellAdd_Yaxis As Variant
    Dim cnt As Integer
    Dim ws As Worksheet
    cnt = 0

    ' make sure a range is selected
    If TypeName(Selection) <> "Range" Then Exit Sub
    Set ws = ActiveSheet

     'For Each cnt In Selection
     For Each MultiSel In Selection
     cnt = cnt + 1

     'ActiveSheet.ChartObjects("Chart USA").Activate
     Sheets("ChartUSASX").ChartObjects("Chart USA").Activate
     ActiveChart.PlotArea.Select

     With ActiveChart.SeriesCollection.NewSeries
         .Name = "=ws!" & MultiSel.Address(ReferenceStyle:=xlA1)
         .XValues = "='US Sector (2)'!" & MultiSel.Offset(0, 24).Address(False, False)
         .Values = "='US Sector (2)'!" & MultiSel.Offset(0, 25).Address(False, False)

        .MarkerSize = 10
        .ApplyDataLabels
        .DataLabels.Select
         Selection.ShowSeriesName = True
        Selection.ShowValue = False
        .MarkerSize = 10

     End With


    Next

End Sub

1 个答案:

答案 0 :(得分:0)

Sub ChartUSA()

    Dim MultiSel As Range
    Dim cht As Chart

    If TypeName(Selection) <> "Range" Then Exit Sub

    Set cht = Sheets("ChartUSASX").ChartObjects("Chart USA").Chart

    For Each MultiSel In Selection.Cells
        With cht.SeriesCollection.NewSeries
            .Name = "=" & RangeAddress(MultiSel)
            .XValues = "=" & RangeAddress(MultiSel.Offset(0, 1))
            .Values = "=" & RangeAddress(MultiSel.Offset(0, 2))
            .MarkerSize = 10
            .ApplyDataLabels
            With .DataLabels
                .ShowSeriesName = True
                .ShowValue = False
            End With
            .MarkerSize = 10
        End With
    Next
End Sub

Function RangeAddress(rng As Range) As String
    RangeAddress = "'" & rng.Parent.Name & "'!" & rng.Address(False, False)
End Function