2012-11-02 23 views
0

我有一個超過300個記錄的Excel文檔。如何用相應的圖像替換文本

在每條記錄的第一行中有一個用戶圖像變量。 此記錄將始終爲username.jpg或爲空。

我有一個文件夾與相應的圖像,所有命名方式完全相同。

是否有任何方法可以自動將第一行中的文本替換爲相關圖像?

+0

應該像if(condition)column.row.loadimage()一樣簡單。嘗試使用excel的visual basic進行一些研究。我不能幫助你,因爲我不會說VB。 – user1534664

+0

我想這個鏈接是非常接近的: http://stackoverflow.com/questions/7382739/embed-image-to-excel-spreadsheet-vba –

+0

xD需要在45分鐘內完成此...我是一個C#人..我也在Excel中尋找一種方法來做到這一點。我已經從XML生成Excel表格。 – TheGeekZn

回答

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