Hello,
I am trying to create new excel file where I need to import picture file based on cell vale.
for example: at cell A1 I will input item number (i.e. 12345, 23456, 34567 etc.) and at cell B1 or anywhere in that spreadsheet I need associated picture.
I will have all associated pictures in one folder with picture name same (i.e. 12345, 23456, 34567 etc.): C:\Documents and Settings\User\Desktop\Pictures
How to go from here?
Hello kaypat,
This macro should get you started. This will import the file using the name in the active cell. If the file is found in the specified folder, it will be inserted with the upper left corner of the picture in the cell to right of the active cell. The macro does not size the picture. It is inserted at full size. Als, the macro expects the picture to have a jpg extension. If your pictures have a different extension, it will need to be changed in the macro code.
Sub ImportPicture() Dim Filepath As String Dim Pic As Object Filepath = "C:\Documents and Settings\User\Desktop\Pictures\" If Dir(Filepath & ActiveCell & ".jpg") = "" Then Exit Sub Set Pic = ActiveSheet.Pictures.Insert(Filepath & ActiveCell) With Pic .Left = ActiveCell.Offset(0, 1).Left .Top = ActiveCell.Offset(0, 1).Top End With End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Thanks Leith. I will try this.
Question: In macro instead of ActiveCell, should I put cell location (i.e. A1 or B1)?
If not then, what formula should I put where I want picture?
Thanks again.
Hello kaypat,
I used ActiveCell only as example. If you have a specific cell in mind, say "A1" with the file path and name, then the code would be as below. This will place the upper left corner of the picture in cell "B1".
Sub ImportPicture() Dim Filepath As String Dim Pic As Object Filepath = "C:\Documents and Settings\User\Desktop\Pictures\" If Dir(Filepath & ActiveCell & ".jpg") = "" Then Exit Sub Set Pic = ActiveSheet.Pictures.Insert(Filepath & ActiveCell) With Pic .Left = Range("A1").Offset(0, 1).Left .Top = Range("A1").Offset(0, 1).Top End With End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Hello kaypat,
It appears that the Picture class has been dropped from Excel starting with Excel 2007. Here is another way that should work.
Sub ImportPicture() Dim Filename As String Dim Filepath As String Dim Pic As Shape Filepath = "C:\Documents and Settings\User\Desktop\Pictures\" Filename = Filepath & ActiveCell & ".jpg" If Dir(Filename) = "" Then Exit Sub With Range("A1").Offset(0, 1) Set Pic = ActiveSheet.Shapes.AddPicture(Filename, msoFalse, msoTrue, .Left, .Top, -1, -1) End With End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Great. This works!
Thank you again!!
I have couple of questions if that's not too much to ask
Can macro re-size picture automatically to fit within certain rows & column or in text-box?
Can this macro run automatic (i.e. as soon as I enter value in cell A1 it imports picture)?
Also, when I ran macro using one cell value (A1) it imported associated pic and when I changed cell value it imported new cell value picture but over old pic (i.e. it did not over-write on old pic or it did not delete old pic before importing new pic)? Is it possible to delete old cell value pic while importing new cell value pic?
Hello kaypat,
Yes, yes, and yes. Would you like me change the macro to do this? My guess is yes.
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
I would be really grateful if you would do that for me?
Hello kaypat,
Here is the revised macro to copy the image from a file into a range of cells. The previous picture will be deleted before the new picture is added.
The attached workbook will import the picture using the contents of cell "A1" on "Sheet1" as the file name. The macro assumes all files are a jpg type so do not add ".jpg" to the file name. The picture range is cells C2 to E14. The picture will be sized automatically to fit the cells in the range.
There are now two macros. One is the Worksheet_Change event for "Sheet1" which checks that you entered a value into cell and calls the import picture macro. The import picture macro then adds the picture from the file to the worksheet and resizes it to fit the range.
Worksheet_Change Event Macro Code
Import Picture To Range Macro CodePrivate Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.Count > 1 Then Exit Sub If Target.Address <> "$A$1" Then Exit Sub Call ImportPicture(Target.Value) End Sub
Sub ImportPicture(ByVal Filename As String) Dim Filepath As String Dim Pic As Shape Dim PicName As String Dim PicRange As Range Set PicRange = ActiveSheet.Range("C2:E14") PicName = PicRange.Cells(1, 1) If PicName <> "" Then ActiveSheet.Shapes(PicName).Delete: PicRange.Cells(1, 1) = "" Filepath = "C:\Documents and Settings\User\Desktop\Pictures\" Filename = Filepath & ActiveCell & ".jpg" If Dir(Filename) = "" Then Exit Sub With PicRange Set Pic = ActiveSheet.Shapes.AddPicture(Filename, msoFalse, msoTrue, .Left, .Top, .Columns.Width, .Rows.Height) PicRange.Cells(1, 1) = Pic.Name End With End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Hey Leith,
Your attached file doesn't work for me?
All I did was changed file path to where I have pics located but it does not do anything! What am I doing wrong?
Hello Leith,
I was finally able to figure it out.
As when you type cell value at A1 and enter.....macro would run automatically to import picture per your code however when ImportPicture macro runs active cell now is A2 (since I hit enter after typing value)......it did not import picture!
What I did is defined cell value A2 equals A1 and highlighted in white you so don't see it..........with this trick macro does it trick per your code!!
Thank you for your help with this code!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks