Hello everyone,
I learnt lots of things from this forum and finally signed up.
I will try to explain what I want to do and what is the problem.
I have a picture in excel sheet and there are equal grids on the picture. ( Pic is gridded with worksheets gridlines) Macro starts if user click on the pic. In this point, I am getting coordinates of the cursor and I know where user clicked. After it, macro copies a prepared shape and puts it to where user clicks.
At this point, I need to know, on which cell is shape located. I found out a code "topleftcell.address" but, It doesn't work correctly. When user clicks an area near corners (especially topleftcorner of grid), code turns with wrong address. I need exactly, "on which cell is shape's center located".
As you see on the picture when I click on the near top left corner of F3, it returns with E3 (You can see the result at M1)
It must return with F3 , that is what I want.
I hope I could explain my problem. Thanks for help.
That is the code I use:
I learnt lots of things from this forum and finally signed up.
I will try to explain what I want to do and what is the problem.
I have a picture in excel sheet and there are equal grids on the picture. ( Pic is gridded with worksheets gridlines) Macro starts if user click on the pic. In this point, I am getting coordinates of the cursor and I know where user clicked. After it, macro copies a prepared shape and puts it to where user clicks.
At this point, I need to know, on which cell is shape located. I found out a code "topleftcell.address" but, It doesn't work correctly. When user clicks an area near corners (especially topleftcorner of grid), code turns with wrong address. I need exactly, "on which cell is shape's center located".
As you see on the picture when I click on the near top left corner of F3, it returns with E3 (You can see the result at M1)
It must return with F3 , that is what I want.
I hope I could explain my problem. Thanks for help.

That is the code I use:
Code:
Sub Rectangle7_Click() Dim hdc As Long
Dim PointsPerPixelX As Double, PointsPerPixelY As Double
Dim CursorPos As POINTAPI
Dim ExcelPos As POINTAPI
Dim ShapePos As POINTAPI
Dim a, b As Long
'Get number of points per screen pixel, depending on screen device size
hdc = GetDC(0)
PointsPerPixelX = 72 / GetDeviceCaps(hdc, LOGPIXELSX)
PointsPerPixelY = 72 / GetDeviceCaps(hdc, LOGPIXELSY)
ReleaseDC 0, hdc
'Scale points per pixel according to current window zoom. The smaller the zoom, the higher the number of points per pixel
PointsPerPixelX = PointsPerPixelX * 100 / ActiveWindow.Zoom
PointsPerPixelY = PointsPerPixelY * 100 / ActiveWindow.Zoom
'Get position of Excel window in screen pixels
ExcelPos.X = ActiveWindow.PointsToScreenPixelsX(0)
ExcelPos.Y = ActiveWindow.PointsToScreenPixelsY(0)
'Get mouse cursor position in screen pixels
GetCursorPos CursorPos
'Set shape position according to mouse position relative to Excel window position, scaled to the
'number of points per pixel. Since the AutoShape's position is defined by the top left corner
'of its bounding box, subtract half the shape's size to centre it over the mouse
ShapePos.X = (CursorPos.X - ExcelPos.X) * PointsPerPixelX '- SHAPE_WIDTH / 2
ShapePos.Y = (CursorPos.Y - ExcelPos.Y) * PointsPerPixelY '- SHAPE_HEIGHT / 2
Application.ActiveSheet.Shapes("Oval 2").Select
With Selection
.Left = ShapePos.X - 2
.Top = ShapePos.Y - 2
End With
Range("m1") = Selection.TopLeftCell.Address
End Sub
Last edited: