通过宏代码动态加载图片
如果需要直接在单元格中显示图片,可以使用 VBA 宏代码。
步骤
-
打开开发工具:
- 如果没有启用开发工具,请点击
文件 > 选项 > 自定义功能区,勾选开发工具。
- 如果没有启用开发工具,请点击
-
添加宏代码:
-
按
Alt + F11打开 VBA 编辑器。 -
点击
插入 > 模块,粘贴以下代码:vba 复制代码 Sub InsertImageFromURL() Dim ws As Worksheet Dim cell As Range Dim pic As Picture Dim picUrl As String ' 指定工作表 Set ws = ThisWorkbook.Sheets(1) ' 遍历选中的单元格 For Each cell In Selection picUrl = cell.Value If picUrl <> "" Then ' 插入图片 On Error Resume Next Set pic = ws.Pictures.Insert(picUrl) If Not pic Is Nothing Then ' 调整图片大小并移动到单元格 With pic .Left = cell.Left .Top = cell.Top .Width = cell.Width .Height = cell.Height End With End If End If Next cell End Sub
-
-
运行宏:
- 回到 Excel,选中包含图片链接的单元格范围。
- 按
Alt + F8,选择InsertImageFromURL,点击运行。
-
效果:
- 每个单元格的图片链接将被替换为实际图片。

浙公网安备 33010602011771号