如何将外部图像加载到 Excel 中?

Coc*_*Dev 4 microsoft-excel

我有一个表格,其中有一列指向图像的链接(URL)。我想将链接的图像加载到下一个单元格中。

例如:

A1 包含

http://example.com/image.jpg

我想将图像加载到B1.

我该怎么做?

tey*_*lyn 10

您不能将图片“加载到”Excel 单元格中。图像对象位于单元格上方的图层上。

您可以使用宏循环遍历单元格并将图像与下一列中的单元格对齐。例如,选择所有图像 URL 然后运行

For Each cel In Selection
    cel.Offset(0, 1).Select
    ActiveSheet.Pictures.Insert(cel.Value).Select
Next cel
Run Code Online (Sandbox Code Playgroud)

不过,图像不会“在”单元格中,但它们的左上角将与 URL 旁边的单元格的左上角对齐。


chu*_*uff 5

虽然图像不能直接加载到单元格中,但可以使用 VBA将图像加载到单元格注释中。此站点中的以下代码定义了一个函数,可在工作表中使用该函数将图像插入到注释中。

  Option Explicit

  Function InsertCommentImage(title As String, absoluteFileName As String)
     Dim commentBox As Comment

   ' Clear any comments before attempting to add them.
     '.ActiveCell.ClearComments // incorrect
     ActiveCell.ClearComments  // works!

   ' Define the comment as a local variable and assign the file name from the
   ' cellAddress input parameter to the comment of a cell.
     Set commentBox = Application.ActiveCell.AddComment
     With commentBox
        .Text Text:=""
        With .Shape
           .Fill.UserPicture (absoluteFileName)
           .ScaleHeight 3, msoFalse, msoScaleFromTopLeft
           .ScaleWidth 2.4, msoFalse, msoScaleFromTopLeft
        End With

      ' Set the visible to True when you always want the image displayed, and
      ' to False when you want it displayed only when you click on the cell.
      .Visible = False
     End With
     InsertCommentImage = title
  End Function
Run Code Online (Sandbox Code Playgroud)

这个 InsertCommentImage 函数有两个参数:一个将出现在函数输入的单元格中的标题,以及对图像位置和名称的引用。

图像可以来自本地文件...

在此处输入图片说明

...或网址...

在此处输入图片说明

...或对文件位置或 Web 链接的引用。

在此处输入图片说明

要使用链接将该函数安装到工作簿中,首先从主功能区中选择“开发人员”选项卡,然后选择“Visual Basic”(“开发人员”选项卡左侧的第一个按钮)。将出现 VBA 集成开发环境 (IDE) 窗口。

然后确保在“项目 - VBA 项目”窗格(左上窗格)中突出显示“VBAProject(您的工作簿名称)”。

从 VBA IDE 主菜单中,选择插入/模块并将函数代码粘贴到已打开的大代码窗格中。

关闭 VBA IDE 窗口并保存工作簿。然后,您可以通过链接使用工作表中的函数。