+ Reply to Thread
Results 1 to 6 of 6

Macro that will insert and resize picture in selected cell range

  1. #1
    Registered User
    Join Date
    09-10-2014
    Location
    NYC
    MS-Off Ver
    2007
    Posts
    3

    Macro that will insert and resize picture in selected cell range

    Hi,

    I am looking for a macro that will insert and automatically resize the image into the cell range I select. I have been using the below code which works great but it is specific to the range I input into the macro. Is there any way the macro can realize the cells I have selected to paste the photo in and adjust the range on its own.


    Sub TestInsertPictureInRange()
    Dim picToOpen As String
    picToOpen = Application _
    .GetOpenFilename("Pics (*.jpg), *.jpg")
    If picToOpen <> "" Then InsertPictureInRange picToOpen, _
    Range("B5:D10")
    End Sub

    Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
    ' inserts a picture and resizes it to fit the TargetCells range
    Dim p As Object, t As Double, l As Double, w As Double, h As Double
    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
    If Dir(PictureFileName) = "" Then Exit Sub
    ' import picture
    Set p = ActiveSheet.Pictures.Insert(PictureFileName)
    ' determine positions
    With TargetCells
    t = .Top
    l = .Left
    w = .Offset(0, .Columns.Count).Left - .Left
    h = .Offset(.Rows.Count, 0).Top - .Top
    End With
    ' position picture
    With p
    .Top = t
    .Left = l
    .Width = w
    .Height = h
    End With
    Set p = Nothing
    End Sub



    Thanks for any help

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro that will insert and resize picture in selected cell range

    Hi MH,

    You need to put code tags around your code - just highlight the code and click on the #

    Maybe:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    09-10-2014
    Location
    NYC
    MS-Off Ver
    2007
    Posts
    3

    Re: Macro that will insert and resize picture in selected cell range

    Thank you for replying, can you repost the code with the correction, I am not sure I am debugging as you suggested... thank you.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro that will insert and resize picture in selected cell range

    Please Login or Register  to view this content.
    *You'd need to have a range selected for this.

    Thanks for the rep!
    Last edited by xladept; 09-10-2014 at 11:15 PM.

  5. #5
    Registered User
    Join Date
    09-10-2014
    Location
    NYC
    MS-Off Ver
    2007
    Posts
    3

    Re: Macro that will insert and resize picture in selected cell range

    Thank you Very much. I found the problem along with your solution xladept. I will post the complete code for anyone who may need it. Thanks.

    Sub TestInsertPictureInRange()
    Dim picToOpen As String
    picToOpen = Application _
    .GetOpenFilename("Pics (*.jpg), *.jpg")
    If picToOpen <> "" Then _
    InsertPictureInRange picToOpen, Selection
    End Sub

    Sub InsertPictureInRange(PictureFileName As String, TargetCells As range)
    ' inserts a picture and resizes it to fit the TargetCells range
    Dim p As Object, t As Double, l As Double, w As Double, h As Double, r As Integer
    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
    If Dir(PictureFileName) = "" Then Exit Sub
    ' import picture
    Set p = ActiveSheet.Pictures.Insert(PictureFileName)
    ' determine positions
    With TargetCells
    t = .Top
    l = .Left
    w = .Width
    h = .Height
    End With
    ' position picture
    With p
    .Top = t
    .Left = l
    .Width = w
    .Height = h
    End With
    Set p = Nothing
    End Sub

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro that will insert and resize picture in selected cell range

    Hi MH,

    Glad that you got it!

    But, you still need to put code tags around your code - you need only to highlight the code and click on the #.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Insert image and resize to fit merges selected cell (also merged cell)
    By acecarwash in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-14-2014, 09:55 AM
  2. Insert picture and resize to range of cells
    By ebahr in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-16-2013, 04:17 PM
  3. [SOLVED] How do I insert a value in each cell of a selected range using macro?
    By alrikvincent in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-31-2013, 05:56 AM
  4. [SOLVED] Excel Insert and Resize Picture Macro Problem in 2007
    By pinstripe05 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-07-2012, 10:40 PM
  5. Insert picture and auto rotate and resize to cell range dimensions
    By jwagman1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-01-2012, 03:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1