Forum Discussion

plotnik25's avatar
plotnik25
Copper Contributor
Nov 27, 2019

Excel coordinats of cell midpoint

Hello, I would like to know, if it is possible to get the coordinates (x/y) of a certain cell in Excel, preferable using vba. Background: I want to center a form around the cell.

Thank you for your support! Gerd

2 Replies

  • JKPieterse's avatar
    JKPieterse
    Silver Contributor

    plotnik25 That is pretty complex. You can get close by getting the top and left and width and height of the cell, but those are relative to the top-left corner of the Excel application window whereas the top and left of a userform are relative to the top-left corner of the display. If accuracy is not important you could just add a fixed amount to cater for the ribbon's size and the window's border width. You'll have to multiply by points per pixel or vice versa as well. And if the Excel window has a split or frozen panes it will complicate matters even more 🙂

  • Hi,
    It is possible to get the 'Top' and 'Left' position (in points) for individual cells and ranges.
    These positions can then be used for positioning.

     

     

    Sub AddShape()
    Dim wsh As Excel.Worksheet
       Set wsh = ActiveSheet
       Call wsh.Shapes.AddShape(Type:=MsoAutoShapeType.msoShapePentagon, _
                                Left:=Range("B2").Left, _
                                Top:=Range("B2").Top, _
                                Width:=120, _
                                Height:=120).Select
    End Sub

     

     

    You could use these 'Top' and 'Left' values to help position a userform over your worksheet.

     

     

    Private Sub UserForm_Initialize()
       'assuming you changed the StartUpPosition property to 'Manual'
       'assuming the default font and assuming the ribbon tab is displayed
       Me.Left = Range("C3").Left + 15
       Me.Top = Range("C3").Top + 170
    End Sub

     

     

    Hope this helps
    Russell

Resources