VBA - Scatterplot labels - images instead of "text"

mrgrumpball

New Member
Joined
Feb 27, 2020
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I have been regularly using the scatterplot in excel, with the well working macro to add labels to (https://support.microsoft.com/en-us...-labels-to-data-points-in-an-xy-scatter-chart) markers. Text is just sitting in the column left to the coordinates. This works perfectly for text.

I am now looking for an adaptation of the macro, where
  • "images" would be inserted in the lable instead of the text,
  • source of the images: a list of images in a specific location (local drive, or url). Generrally, there are about 70 to 100 images to insert.
  • Format would be 80x50 pixels.
  • Ideally the size of the label box would also be adapted in the so the image does not get distorted

An adaptation that was suggested but does not seems to work was based on userpicture (Scatter Plot Labeling with Images). This returns an error. Can anyone suggest the changes to makes

Let's say that the path and filename for your images are located in the corresponding cells one column to the right of the series values, replace...




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

with

For Counter = 1 To Range(xVals).Cells.Count
With ActiveChart.SeriesCollection(1).Points(Counter)
.HasDataLabel = True
With .DataLabel.Format.Fill
.UserPicture Range(xVals).Cells(Counter, 1).Offset(0, 1).Value
.Visible = msoTrue
End With
End With
Next Counter
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Welcome to MrExcel forums.

What's the error and which line causes it? Always post these details because it helps people to help you.

Following the MS example, put the full path and file name of each image in column D (D2:D6), then run this macro with the Chart1 sheet active:

VBA Code:
Public Sub AttachImagesToPoints()

    Dim i As Long, xVals As String
    
    'Disable screen updating while the subroutine is run.
    Application.ScreenUpdating = False
    
    'Store the formula for the first series in "xVals".
    xVals = ActiveChart.SeriesCollection(1).Formula
    
    'Extract the range for the data from xVals.
    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
    
    'Attach an image to each data point in the chart.
    For i = 1 To Range(xVals).Cells.Count
        With ActiveChart.SeriesCollection(1).Points(i)
            .HasDataLabel = True
            .DataLabel.Text = ""
            With .DataLabel.Format.Fill
                .UserPicture Range(xVals).Cells(i, 1).Offset(0, 2).Value
                .Visible = msoTrue
            End With
        End With
    Next
 
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,639
Messages
6,125,970
Members
449,276
Latest member
surendra75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top