Can anyone show on how can i move an image on an excel sheet to a specific cell.

Thank you in advance.

Recommended Answers

All 7 Replies

This is code I use in one of my Excel applications. The trick is the AddPicture method that requires a top, left, width and height. These values are picked up from the Range (D2 in this case). Since my image is not the shape of the cell some adjustments need to be made, hence the width times 0.25 and so on.
All four variables are declared as single.

        With .Range("D2")
            L = .Left + .Width / 4
            T = .Top
            W = .Width * 0.25
            H = .Height * 1.25
        End With    ' range d2
        Call .Shapes.AddPicture("C:\Path\MyImage.Jpg", msoTrue, msoTrue, L, T, W, H)

Thank you sir, What if the image from d2 i want to move it to d10 is it possible?

Place the picture on the sheet and right-click it.Pick Size and Properties.Size it to fit with a cell. Move it to its proper place.Go back to Size and Properties and set the Properties to move and resize with cell.

If you want to move it with code the following will Place the picture then move the picture then remove the picture.
Naming the picture when you place it on the sheet seems to be the easiest way to make a reference to it later, unless someone else has a better way.

    Option Explicit
    Dim MyPath As String
    Dim L As Single
    Dim T As Single
    Dim W As Single
    Dim H As Single
    Private Sub CommandButton1_Click()
    ' Place picture on worksheet at a particular cell
        MyPath = "C:\MyPath\APicture.jpg"
        With Sheet1.Range("C5")
            L = .Left
            T = .Top
            H = .Height * 2
            W = .Width * 2
        End With
        Call Sheet1.Shapes.AddPicture(MyPath, msoTrue, msoTrue, L, T, W, H)
        'If you don't name the picture right here it will be difficult to find later
        Sheet1.Shapes.Item(Shapes.Count).Name = "MyPicture"
        CommandButton2.Enabled = True
    End Sub

    Private Sub CommandButton2_Click()
    ' Move picture to a given cell

        With Sheet1
            With .Range("G7")
                T = .Top
                L = .Left
            End With
            With .Shapes.Item("MyPicture")
                .Top = T
                .Left = L
            End With
        End With
    End Sub

    Private Sub CommandButton3_Click()
    ' Remove picture from sheet
        Sheet1.Shapes.Item("MyPicture").Cut
    End Sub

Thank you sir. Whatif the image is existing my image is existing. I'm trying to createa chess game with an AI via excel.

Thank you sir.

The following code will step through all the shapes on the sheet

See if this helps:
If the current name of the Item is Picture then it makes the picture twice as wide, showing which picture is selected.
An input box collects the name such as RedKingKnight and assigns the name to the Item; then makes the picture the original width.
The good news is you only have to do this once.

Private Sub CommandButton4_Click()
Dim Counter As Integer
Dim NextName As String
    For Counter = 1 To Shapes.Count
    With Sheet1.Shapes
        Debug.Print .Item(Counter).Name
        If Left$(.Item(Counter).Name, 7) = "Picture" Then
            .Item(Counter).Width = .Item(Counter).Width * 2
            NextName = InputBox("Current Name is" & .Item(Counter).Name, "Rename Pictures")
            .Item(Counter).Name = NextName
            .Item(Counter).Width = .Item(Counter).Width / 2
        End If
    End With
    Next Counter
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.