Home » excel » vba – Define the position of object in excel sheet

vba – Define the position of object in excel sheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

Issue: Put the object’s (here is the circle) contents into the corresponding cell. (On the picture – put “5” into E3 cell, and also put second “5” into F3)
excel table

I have some experience in VBA-programming (for excel).
And all I know – this is impossible to do. We can iterate through all the objects on the sheet, get it’s content, but we still can not define it’s (the objects’) coordinate.
So this issue is unsolvable.

But I decided to ask the SO community, because I may be wrong.
If you give me some info – I’ll appreciate that.

NB. I’m not asking to solve my “university’s problem”. I just want a hint, where I should look or something.

How to&Answers:

If I understand you correctly, you want to return the cell location that the shape is positioned at? You can display the cell address of the top left corner of the shape like this:

Sub getLocation()
    Dim wks As Worksheet
    Set wks = Sheets("Sheet1")

    For Each sShapes In wks.Shapes
        MsgBox (sShapes.TopLeftCell.Address)
    Next
End Sub

You can also move the shape to a specific cell like this:

Sub getLocation()
    Dim wks As Worksheet
    Set wks = Sheets("Sheet1")

    'move shape 'Oval 1' to cell H3
    wks.Shapes("Oval 1").Left = wks.Cells(3, 8).Left
    wks.Shapes("Oval 1").Top = wks.Cells(3, 8).Top
End Sub