0

I have an excel document with over 300 records.

In the first row for each record, there is a User Image variable. This record will always be username.jpg or empty.

I have a folder with the corresponding images, all named in the exact same fashion.

Is there any way to replace the text in the first row with their related images automatically?

4

1 に答える 1

0

If your User image variable is in column A, then this code will insert the picture into column A and adjust the height of the row to the height of the picture. You will probably have to tweak the code slightly.

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 に答える