Option Explicit
#If Win64 Then
Const NULL_PTR = 0^
#Else
Const NULL_PTR = 0&
#End If
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
#If VBA7 Then
Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hUF As LongPtr) As Long
Private Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal x As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
#Else
Private Enum LongPtr
[_]
End Enum
Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hUF As LongPtr) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal x As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
#End If
Public Sub Show_and_Position_Userform(form As UserForm, cell As Range)
Const SWP_NOSIZE = &H1, SWP_NOACTIVATE = &H10, SWP_SHOWWINDOW = &H40
Dim hForm As LongPtr, tTargetRect As RECT
Call IUnknown_GetWindow(form, VarPtr(hForm))
tTargetRect = GetRangeRect(cell)
With tTargetRect
Call SetWindowPos(hForm, NULL_PTR, .Left, .Top, 0&, 0&, SWP_SHOWWINDOW + SWP_NOSIZE)
End With
End Sub
Private Function GetRangeRect(ByVal obj As Object) As RECT
Dim oPane As Pane
Set oPane = ThisWorkbook.Windows(1&).ActivePane
With GetRangeRect
.Left = oPane.PointsToScreenPixelsX(obj.Left - 1&)
.Top = oPane.PointsToScreenPixelsY(obj.Top)
.Right = oPane.PointsToScreenPixelsX(obj.Left + obj.Width)
.Bottom = oPane.PointsToScreenPixelsY(obj.Top + obj.Height)
End With
End Function