Friday, March 14, 2014

add a button in Excel to insert a picture through a macro

This will ensure that the picture is embedded/saved in the workbook and
can also be called if the worksheet is locked.

You will need to edit the VB script to the Range you want the picture inserted into.

First off, you need to enable the Develop tab on the ribbon:






after that, add a button to your excel spreadsheet:





then you can copy and paste (and edit) the VB script:








then create a new macro called addpicture (or whatever)





and then the code:

Sub addpicture()

     Dim filename As String

     filename = "invalid"

     filename = Application.GetOpenFilename(FileFilter:="Pictures(*.jpg), .jpg", _
                                             Title:="Insert Picture...", _
                                             MultiSelect:=False)
     If filename = "invalid" Or filename = "False" Then
         Exit Sub
     End If

     Range("C17", "E27").Select

     Sheets(1).Shapes.addpicture _
             filename, _
             False, _
             True, _
             Sheets(1).Range("C17", "E27").Left, _
             Sheets(1).Range("C17", "E27").Top, _
             Sheets(1).Range("C17", "E27").Width, _
             Sheets(1).Range("C17", "E27").Height


End Sub



you need to change Range("C17","E27") to whatever cell range to put the picture into.

No comments:

Post a Comment