3

散布図のデータ ラベルを各ポイントのタイトルに変更するマクロを作成しようとしています。例えば:

グラフの例
(出典: wiseowl.co.uk )

この場合、データ ラベルに映画のタイトルを表示したいと考えています。私が使用したコードはこのウェブサイトからのものですが、私の散布図には 2 つの一連のデータがあります。動的な名前付き範囲を使用して、名前があるセルの範囲をマクロに伝えたいと思います。私のコードでは、動的な名前付き範囲は FONames と BONames です。これが私のコードです:

Sub CreateDataLabels()
'holds the entire film data series in the chart
Dim FilmDataSeries As Series
Dim FilmDataSeries2 As Series
'holds one cell at a time
Dim SingleCell As Range
'holds the full list of cells containing film names
Dim FilmList As Range
Dim FilmList2 As Range
'keeps track of which datapoint we're labelling
Dim FilmCounter As Integer
Dim FilmCounter2 As Integer
'The sheet in which the graph is on
Dim ws As Worksheet
Set ws = Worksheets("Datos Gráfico")

'set the counter to start at 1
FilmCounter = 1
FilmCounter2 = 1

'set a reference to the cells containing the list of films
Set FilmList = Range("FONames")
Set FilmList2 = Range("BONames")

'set a reference to the chart data series
Set FilmDataSeries = ws.ChartObjects(1).Chart.SeriesCollection(1)
Set FilmDataSeries2 = ws.ChartObjects(1).Chart.SeriesCollection(2)

'make sure data labels are turned on
FilmDataSeries.HasDataLabels = True
FilmDataSeries2.HasDataLabels = True

'loop over the cells in the list of films
For Each SingleCell In FilmList
    FilmDataSeries.Points(FilmCounter).DataLabel.Text = SingleCell.Value
    FilmCounter = FilmCounter + 1
Next SingleCell

For Each SingleCell In FilmList2
    FilmDataSeries2.Points(FilmCounter2).DataLabel.Text = SingleCell.Value
    FilmCounter2 = FilmCounter2 + 1
Next SingleCell

End Sub

指定されたディメンションが無効であることを示す 1004 エラーが発生します。FONames または BONames を実際のセル範囲に置き換えると、たとえば。Range("A5","A11")、すべてが完全に機能します。これらの動的な名前付き範囲をコードで使用するにはどうすればよいですか?

4

2 に答える 2

0

動的な名前付き範囲の定義は何ですか?次のように、列Aの名前付き範囲を定義しました。

=OFFSET(Sheet1!$A$1,1,0,COUNTA(Sheet1!$A:$A)-1,1)

そしてそれはうまくいきました。

于 2012-11-14T21:12:56.730 に答える
0

例えば ​​:

'---------------------------------------------------------------------------
' In order to add label at scatter graph. You must selected a graph before.
' @param void
'---------------------------------------------------------------------------
Sub add_label_on_graph()
    Dim Counter As Integer
    Dim xVals As String
    Dim reponse As Integer

    If ActiveChart Is Nothing Then
        reponse = MsgBox("Attention, vous devez au préalable sélectionner un graphique avant de lancer cette commande.", vbExclamation, "Information")
        Exit Sub
    End If

    Application.ScreenUpdating = False

    ' Récupération de la range des données que l'on stocke dans la variable xVals.
    xVals = ActiveChart.SeriesCollection(1).Formula
    xVals = Mid(xVals, InStr(InStr(xVals, ","), xVals, Mid(Left(xVals, InStr(xVals, "!") - 1), 9)))
    xVals = Left(xVals, InStr(InStr(xVals, "!"), xVals, ",") - 1)
    Do While Left(xVals, 1) = ","
        xVals = Mid(xVals, 2)
    Loop

    ' Ajoute un label à chaque point du graphique
    For Counter = 1 To Range(xVals).Cells.Count
        ActiveChart.SeriesCollection(1).Points(Counter).HasDataLabel = True
        ActiveChart.SeriesCollection(1).Points(Counter).DataLabel.text = Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
    Next Counter

    Application.ScreenUpdating = True
End Sub
于 2014-11-13T13:17:55.310 に答える