0

I have a code for inserting picture inside an active cell but I couldn't figure out a way on how to modify it to insert the picture to be only in column D cells rather than active cell.

Sub InsertPictureInActiveCell()
Dim strFile As String
Dim rng As Range
Dim sh As Shape
Const cFile As String = "Image Files(*.bmp;*.jpg;*.jpeg;*.png),"
strFile = Application.GetOpenFilename(fileFilter:=cFile, Title:=Es)
If strFile = "False" Then
Else
    Set rng = ActiveCell
    Set rng = rng.MergeArea
    With rng
        Set sh = ActiveSheet.Shapes.AddPicture(Filename:=strFile, 
        linkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, 
        Top:=.Top, Width:=.Width, Height:=.Height)
        sh.LockAspectRatio = msoFalse
        sh.Placement = xlMoveAndSize
        End With
        Set sh = Nothing
        Set rng = Nothing

  End If
 End Sub

I'm still new to VBA. I would be glad if anyone could help me with this situation.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
RoodyT
  • 23
  • 7
  • You're using `ActiveCell`, so that's where it will go. What range is the user expected to select before running this? – Tim Williams Oct 02 '18 at 00:28
  • The user would have to select cells in column D to insert pictures. I have tried with this line of code sh.Range("D" & n + 1).Value = Me.Image1.Picture but it gave me numbers rather than the picture being inserted in the cell – RoodyT Oct 02 '18 at 00:33
  • Are they supposed to select only a single cell? If you need to ensure the picture is going to the correct cell then something like `Set rng = Selection.Cells(1).EntireRow.Cells(4)` will account for the user selecting (eg) a cell in another column. – Tim Williams Oct 02 '18 at 00:37
  • Yeah, they're supposed to select only a single cell, but I was trying to make that every time they would want to insert another picture, the code would automatically insert the picture down to another row of the same column, which was column D. hence why in my previous comment i included the n+1 in the ("D" & n+1).Value inside the bracket. this was what i was working with n = sh.Range("B" & Application.Rows.Count).End(xlUp).Row 'sh.Range("D" & n + 1).Value = Me.Image1.Picture but this line of code didn't work – RoodyT Oct 02 '18 at 00:58
  • If in addition to placing a picture over the cell you also put a value there, you can use `Set rng = ActiveSheet.cells(rows.count, 4).End(xlUp).Offset(1,0)` That won't work without values in colD though. – Tim Williams Oct 02 '18 at 03:11

0 Answers0