Image from Userform to Sheet

Jimmypop

Well-known Member
Joined
Sep 12, 2013
Messages
753
Office Version
  1. 365
Platform
  1. Windows
Good day all

I have the code below which works and does pass the Image from Image1 control on my userform AddEmp to a sheet called "Pics" but not to the correct position...

The image is loaded and renamed to Image 1 on userform from Textbox 3 and 5. Now when I click to add to database it needs to do the follwong:

1. Take the image and place it in Cell C1 and then In A1 it needs the text in Textbox3 and in A2 it needs text from Textbox5 (Text boxes located on userform AddEmp).
2. Any subsequent entries need to go to next empty row....
3. Image needs to resize to fit inside the cell it is placed in.

VBA Code:
Sub InsertImage(ImageFileName As String, ID As String, Empl As String)
    Dim Image As Object, t As Double, l As Double, w As Double, h As Double, ws As Worksheet, TargetCell As Range
    Set ws = Sheets("Pics")
    If ws Is Nothing Then Exit Sub ' Check if worksheet exists
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Get last used row in column A
    Set TargetCell = ws.Cells(lastRow + 1, 1) ' Set target cell to the next empty row on Pics sheet
    TargetCell = "'" & ID
    TargetCell.Offset(, 1) = Empl
    ' import picture
    Set Image = ws.Pictures.Insert(ImageFileName)
    ' cell position
    With TargetCell
        t = .Top
        l = .Left
        w = .Width
        h = .Height
    End With
    ' image placement
    With Image
        .Top = t
        .Left = l
        .Width = w
        .Height = h
        .Placement = xlMoveAndSize ' resize image to fit within the target cell
        .Name = ID
    End With
    Set Image = Nothing
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi there

I then get an Argument not Optional error on line

VBA Code:
With TargetCell.Range
 
Upvote 0
It does now insert the image, however...

1. A1 it needs the text in Textbox3 and in A2 it needs text from Textbox5 These items instead of A1 and A2 gets placed in B1 and B2. All text after this does go into next empty row... so this looks sorted
2. However all images gets placed in top left corner of cell G1 each time and not C1, C2,C3 etc...
 
Upvote 0
Not sure why image would go to G1? Target cell is set for "A" & lastrow +1.
Code:
With TargetCell.Offset(0,3)
seems like it would place the image in "C"
Dave
 
Upvote 0
That is what is boggling my mind... With code above it now moves the image further to between column M and N so I checked and made pics the size of 200px by 200px and changed

VBA Code:
With TargetCell.Offset(0,3)

to

VBA Code:
With TargetCell.Offset(0,2)

Which did the trick.

But sometimes images are larger than 200 by 200.

can there be a way to make it that size before it is loaded to Image control on usefform or before it goes to the Pics Sheet?
 
Upvote 0
UPDATE:

Got the sizing to work... however some pics are still not placed in rows in column c... some of them are placed perfectly others are still way off...
 
Upvote 0
I guess offset 3 from "A" would be "D" ... still drinking coffee. Maybe...
Code:
With TargetCell.Offset(0,2).Address
Dave
 
Upvote 0
Found the issue... it is when images are loaded to the userform image control.. they are loaded but rotate by itself it is these ones causing issue..will explain some more when at home
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

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