VBA将图像指定给Excel中的散点图

VBA将图像指定给Excel中的散点图,excel,vba,image,charts,Excel,Vba,Image,Charts,我在Excel上有一个散点图。图中的每个点都指向一家公司。我想在绘图中的每个点都填充一个对应于他们所指公司的图像。公司名称位于G列的spreadhseet中,从第3行开始 我已经设法编写了一些VBA,将与公司名称相关的图像加载到Excel中。例如,如果单元格上写有“Microsoft”,脚本将查找具有相同名称的图片,并将其发布到电子表格中的预定义单元格中。 我现在想让脚本用加载的图像填充散点图中的“Microsoft”点 脚本的运行时间与单元格的运行时间相同 Sub Macro2() Dim

我在Excel上有一个散点图。图中的每个点都指向一家公司。我想在绘图中的每个点都填充一个对应于他们所指公司的图像。公司名称位于G列的spreadhseet中,从第3行开始

我已经设法编写了一些VBA,将与公司名称相关的图像加载到Excel中。例如,如果单元格上写有“Microsoft”,脚本将查找具有相同名称的图片,并将其发布到电子表格中的预定义单元格中。 我现在想让脚本用加载的图像填充散点图中的“Microsoft”点

脚本的运行时间与单元格的运行时间相同

Sub Macro2()

Dim picname As String
Dim shp As Shape
Dim pasteAt As Integer
Dim lThisRow As Long
Dim present As String

lThisRow = 3 'This is the start row

Do While (Cells(lThisRow, 7) <> "")


    pasteAt = lThisRow
  Cells(pasteAt, 2).Select 'This is where picture will be inserted (column)


    picname = Cells(lThisRow, 7) 'This is the picture name

    present = Dir("C:\Users\User\Images\" & picname & ".jpg")

    If present <> "" Then

            Cells(pasteAt, 2).Select

            Call ActiveSheet.Shapes.AddPicture("C:\Users\User\Images\" & picname & ".jpg", _
            msoCTrue, msoCTrue, Left:=Cells(pasteAt, 2).Left, Top:=Cells(pasteAt, 2).Top, Width:=100, Height:=100).Select

    End If

       lThisRow = lThisRow + 1
Loop

End Sub
Sub-Macro2()
将picname设置为字符串
将shp变暗为形状
Dim pasteAt为整数
我的头发和你的一样长
以字符串的形式出现
lThisRow=3'这是起始行
执行时(单元格(左下方,7)”)
pasteAt=lthislow
单元格(粘贴,2)。选择“这是插入图片的位置(列)”
picname=Cells(lthislow,7)'这是图片名称
present=Dir(“C:\Users\User\Images\”和picname&“.jpg”)
如有,则
单元格(粘贴,2)。选择
调用ActiveSheet.Shapes.AddPicture(“C:\Users\User\Images\”&picname&“.jpg”_
msoCTrue,msoCTrue,左:=单元格(粘贴,2)。左,顶部:=单元格(粘贴,2)。顶部,宽度:=100,高度:=100)。选择
如果结束
lthistrow=lthistrow+1
环
端接头

现在,我想补充脚本,以便将图像插入到图表中。

您需要循环查看图表中的序列和点。您没有指出数据是如何排列和绘制的,但是我假设图表有一系列的X和Y,公司列与X和Y值平行

我已尝试顺利合并我的添加内容:

Sub ImportPicturesAndPutIntoChart()

  Dim picname As String
  Dim shp As Shape
  Dim lThisRow As Long
  Dim present As String
  Dim cht As Chart, srs As Series

  lThisRow = 3 'This is the start row

  Set cht = ActiveSheet.ChartObjects(1).Chart
  Set srs = cht.SeriesCollection(1)

  Do While (Cells(lThisRow, 7) <> "")
    If lThisRow - 2 > srs.Points.Count Then Exit Do

    Cells(lThisRow, 2).Select 'This is where picture will be inserted (column)
    picname = Cells(lThisRow, 7) 'This is the picture name

    present = Dir("C:\Users\User\Images\" & picname & ".jpg")
    If present <> "" Then

      Cells(pasteAt, 2).Select

      Set shp = Shapes.AddPicture("C:\Users\User\Images\" & picname & ".jpg", _
          msoCTrue, msoCTrue, Left:=Cells(lThisRow, 2).Left, Top:=Cells(lThisRow, 2).Top, _
          Width:=100, Height:=100)

      shp.Copy
      srs.Points(lThisRow - 2).Paste

    End If

    lThisRow = lThisRow + 1

  Loop

End Sub
子导入图片和输入图表()
将picname设置为字符串
将shp变暗为形状
我的头发和你的一样长
以字符串的形式出现
Dim cht作为图表,srs作为系列
lThisRow=3'这是起始行
Set cht=ActiveSheet.ChartObjects(1.Chart)
设置srs=cht.系列集合(1)
执行时(单元格(左下方,7)”)
如果lThisRow-2>srs.Points.Count,则退出Do
单元格(LhisRow,2)。选择“这是插入图片的位置(列)”
picname=Cells(lthislow,7)'这是图片名称
present=Dir(“C:\Users\User\Images\”和picname&“.jpg”)
如有,则
单元格(粘贴,2)。选择
设置shp=Shapes.AddPicture(“C:\Users\User\Images\”&picname&“.jpg”_
msoCTrue,msoCTrue,左:=单元格(lthistrow,2)。左,顶部:=单元格(lthistrow,2)。顶部_
宽度:=100,高度:=100)
复印件
srs.点(lThisRow-2).粘贴
如果结束
lthistrow=lthistrow+1
环
端接头