Jimmypop
Well-known Member
- Joined
- Sep 12, 2013
- Messages
- 753
- Office Version
- 365
- Platform
- 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.
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