0

我有一个包含 300 多条记录的 Excel 文档。

在每条记录的第一行中,有一个用户图像变量。此记录将始终为username.jpg空。

我有一个包含相应图像的文件夹,所有图像都以完全相同的方式命名。

有什么方法可以自动将第一行中的文本替换为相关图像?

4

1 回答 1

0

如果您的用户图像变量在 A 列中,则此代码会将图片插入 A 列并将行高调整为图片的高度。您可能需要稍微调整代码。

Sub insertPic()

Dim pic As String
Dim url As String
Dim r As Integer
Dim he As Integer



url = "C:\User\Pictures\" ' This is the variable to the folder

For r = 1 To 300
pic = Sheets("Sheet1").Range("A" & r).Value ' This is your username file
pic = url & pic



With ActiveSheet.Pictures.Insert(pic) ' Insert picture into active sheet
    .Left = ActiveSheet.Range("A" & r).Left ' left and top align the left and top of the picture with the specified cell
    .Top = ActiveSheet.Range("A" & r).Top
    he = .Height ' get height of image


End With
Sheets("Sheet1").Range("A" & r).Select
    Selection.RowHeight = he ' match row height to picture height
Next r


End Sub
于 2012-11-02T13:49:29.687 回答