1

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

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

我希望能够更改硬编码以允许动态工作表引用。

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

我附上下面的代码以供您参考。现在关于硬编码工作表名称的 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
4

1 回答 1

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
于 2013-08-23T00:14:39.833 回答