+ Reply to Thread
Results 1 to 4 of 4

Using a Userform to Browse image file then paste selected image to sheet?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-22-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    329

    Using a Userform to Browse image file then paste selected image to sheet?

    Hi all,

    I located a code that allows the user to browse image files & then paste that image to the "active" spreadsheet, my problem is I need the file path to go to a specific sheet ("Sheet1") & cell ("A1") within the workbook & to a specific size, is it possible to adapt the code below to suit? Another anticipated problem is if the user want to paste a second image (maximum 2 allowed) to the same sheet in ("A2"). As always guys any advise / guidance will be greatly appreciated, thank you all & have a great day - Marco

    Sub Rectangle1_Click()
    Dim fileToOpen As Variant
    
    fileToOpen = Application _
        .GetOpenFilename("All Picture Files (*.jpg;*.gif;*.bmp;*.tif),*.jpg;*.gif;*.bmp;*.tif")
        
    If fileToOpen <> False Then
        txtFile = fileToOpen
        Module1.insert_pic (fileToOpen)
        ActiveSheet.Shapes("newPic").Left = 0
        Unload Me
    End If
    End Sub
    Option Explicit
    Sub insert_pic(sFilePath As String)
    Dim s As String
    s = sFilePath
    ActiveSheet.Pictures.Insert(s).Name = "newPic"
    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Using a Userform to Browse image file then paste selected image to sheet?

    Try something like this...

    Sub Rectangle1_Click()
        Dim fileToOpen As Variant, rng As Range
        
        fileToOpen = Application _
            .GetOpenFilename("All Picture Files (*.jpg;*.gif;*.bmp;*.tif),*.jpg;*.gif;*.bmp;*.tif")
        
        If fileToOpen <> False Then
            Set rng = Sheets("Sheet1").Range("A1")
            With rng.Parent.Pictures.Insert(fileToOpen)
                .Name = "newPic A1"
                .Left = rng.Left
                .Top = rng.Top
                .Width = rng.Width
                .Height = rng.Height
            End With
        Else
            Unload Me
        End If
        
        If MsgBox("Do you want to insert a 2nd picture? ", vbQuestion + vbYesNo, "Insert Another Pic") = vbYes Then
            
            fileToOpen = Application _
                .GetOpenFilename("All Picture Files (*.jpg;*.gif;*.bmp;*.tif),*.jpg;*.gif;*.bmp;*.tif")
            
            If fileToOpen <> False Then
                Set rng = Sheets("Sheet1").Range("A2")
                With rng.Parent.Pictures.Insert(fileToOpen)
                    .Name = "newPic A2"
                    .Left = rng.Left
                    .Top = rng.Top
                    .Width = rng.Width
                    .Height = rng.Height
                End With
            End If
            
        End If
        
        Unload Me
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    04-22-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    329

    Re: Using a Userform to Browse image file then paste selected image to sheet?

    WOW! That works like a charm! Can't thank you enough AlphaFrog! Marco

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Using a Userform to Browse image file then paste selected image to sheet?

    You're welcome. Thanks for the feedback.

+ 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. Problem with image File Link in Cell not opening image for preview
    By Orada in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-24-2015, 09:33 AM
  2. [SOLVED] How to apply Image borders to an image that my excel vba userform pastes in a word doc?
    By CaptainCool in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-11-2014, 05:40 PM
  3. [SOLVED] VBA to transfer image FROM userform image control TO a worksheet cell
    By Zoediak in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2014, 02:51 PM
  4. [SOLVED] Linking Image to a cell value, and display the selected image in new sheet
    By siva venkata in forum Excel General
    Replies: 12
    Last Post: 03-25-2014, 02:34 AM
  5. Userform image browse folder, show preview and save to range
    By Mehmet82 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-04-2013, 12:47 PM
  6. Replies: 2
    Last Post: 07-02-2013, 03:36 PM
  7. [SOLVED] Insert jpg image from file path to image frame
    By SAsplin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-27-2012, 10:12 AM

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