Insert picture via VBA

pam53154

Board Regular
Joined
Mar 3, 2009
Messages
107
I am trying to write a simple macro to insert a picture - but not well versed with VBA so I'm having some trouble. I am able to insert the picture, but I can't control the cell it pastes to. I want it to paste to the cell I currently have selected, but now it pastes to the same place regardless of the currently selected cell. Any thoughts? Thanks!

Here's what I have. I have also tried minor variations, all of which end up with the picture going to the same constant cell.

ActiveCell.Offset(0, 0).Select
ActiveSheet.Pictures.Insert ( _
"C:\Users\Andrew\Pictures\Test.jpg")
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Replace Range("A1") or Range("B1") with ActiveCell as needed.

Code:
Sub test()
    Dim myFiles, e
    myFiles = Application.GetOpenFilename(, , , , True)
    If Not IsArray(myFiles) Then Exit Sub
    For Each e In myFiles
        With ActiveSheet
            With .Pictures.Insert(e)
                .Left = Range("b1").Left
                .Top = Range("b1").Top
                .Height = Range("a1").Height
                .Width = Range("a1").Width
            End With
        End With
    Next
End Sub
 
Upvote 0
Thanks... this helps! Can you clarify how to replace A1 and B1 with the active cell? Would I need to manually update the code each time to the intended cell, or is there something more fluent I can put in the code which means the active cell (sorry if this is what you meant by ActiveCell... just not real good at this, so if you could show the actual code for an active cell reference, that would be wonderful!). Thanks again!
 
Upvote 0
Code:
Sub test()
    Dim myFiles, e, aCell As Range
    myFiles = Application.GetOpenFilename(, , , , True)
    If Not IsArray(myFiles) Then Exit Sub
    Set aCell = ActiveCell
    For Each e In myFiles
        With ActiveSheet
            With .Pictures.Insert(e)
                .Left = aCell.Left
                .Top = aCell.Top
                .Height = aCell.Height
                .Width = aCell.Width
            End With
        End With
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,877
Members
449,056
Latest member
ruhulaminappu

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