Assign location to picture after import with VBA

Olanda

New Member
Joined
May 9, 2011
Messages
2
first compliments for the board, I get a lot of information from here. Thanks.

I've made a nice macro which imports images from a designated filemap on C:/ into the sheet. The name of the imported files is in Colomn A, and the image must be imported into Colomn B.

The import works fine, all the images are imported into the sheet, but are all stacked together on 1 place (around cell C3). :confused: I want them to be imported into colomn B, in the same row as the file name in Colomn A.

This is the macro code i'm using. I hope somebody can help me.

Code:
Sub Macro1()
    Range("a2").Select
    On Error Resume Next
    
    Do Until ActiveCell.Value = ""
    Afb_naam = "Images\75dpi\" & ActiveCell.Value
    Afb_map = "c:\"
    Afb_bestandsnaam = Afb_map & Afb_naam & ".jpg"
    
    ActiveCell.Offset(0, 1).Select
    
    If Dir(Afb_bestandsnaam) <> "" Then
    ActiveSheet.Pictures.Insert(Afb_bestandsnaam).Name = Afb_naam
    Rows(ActiveCell.Row & ":" & ActiveCell.Row).RowHeight = ActiveSheet.Shapes(Afb_naam).Height
    Else: ActiveCell.Value = "Foto bestaat niet"
    End If
    ActiveCell.Offset(1, -1).Select
    Loop
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Perhaps like this

Rich (BB code):
Sub Macro1()
    Range("a2").Select
    On Error Resume Next
    
    Do Until ActiveCell.Value = ""
    Afb_naam = "Images\75dpi\" & ActiveCell.Value
    Afb_map = "c:\"
    Afb_bestandsnaam = Afb_map & Afb_naam & ".jpg"
    
    ActiveCell.Offset(0, 1).Select
    
    If Dir(Afb_bestandsnaam) <> "" Then
    ActiveSheet.Pictures.Insert(Afb_bestandsnaam).Name = Afb_naam
    Rows(ActiveCell.Row & ":" & ActiveCell.Row).RowHeight = ActiveSheet.Shapes(Afb_naam).Height
    With ActiveSheet.Shapes(Afb_naam)
        .Top = ActiveCell.Top
        .Left = ActiveCell.Left
    End With
    Else: ActiveCell.Value = "Foto bestaat niet"
    End If
    ActiveCell.Offset(1, -1).Select
    Loop
End Sub
 
Upvote 0
Hey VoG,

that was fast, that's the perfect solution for me!

I'm almost ashamed that I couldn't figure this out for myself. Thanks a lot!
 
Upvote 0
Thank you for a very useful topic!

A slight alteration is required to change the height of the image. Should it go to the
"With ActiveSheet.Shapes(Afb_naam)
.Top = ActiveCell.Top ... "
part?

Does the ".Height" property exist, please?
 
Upvote 0
Yes a picture has Height and Width properties, so you can use code like

Code:
    With ActiveSheet.Shapes(Afb_naam)
        .LockAspectRatio = False
        .Top = ActiveCell.Top
        .Left = ActiveCell.Left
        .Height = ActiveCell.RowHeight
        .Width = ActiveCell.Width
    End With
 
Upvote 0
Thank you for the explanation. Please correct me, if I am wrong, the code for me (with image height 50px) should look like:

Code:
    With ActiveSheet.Shapes(Afb_naam)
    [COLOR="Red"]    .LockAspectRatio = True[/COLOR]
        .Top = ActiveCell.Top
        .Left = ActiveCell.Left
       [COLOR="red"] .Height = 50[/COLOR]
    End With
 
Upvote 0

Forum statistics

Threads
1,224,532
Messages
6,179,388
Members
452,908
Latest member
MTDelphis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top