Public cel As Range
Public Function WindowXYFromPoint(x, y): WindowXYFromPoint = ExecuteExcel4Macro("CALL(""user32"",""WindowFromPoint"",""JJJ""," & x & ", " & y & ")"): End Function
Public Function ShowOnCell(cel As Range, Optional modal As Boolean = vbModal)
With UserForm1: Set .cel = cel: .Show modal: End With
End Function
Function correction()
Dim x#, y#, h2&, h1&, PtPx#, EcX&, EcY&
With ActiveWindow.ActivePane
x = .PointsToScreenPixelsX(cel.Left): y = .PointsToScreenPixelsY(cel.Top)
PtPx = (.PointsToScreenPixelsX(72) - .PointsToScreenPixelsX(0)) / 72 'coeff
End With
EcX = Round(Me.Width - Me.InsideWidth) * PtPx 'ecart maximum toléré par la fonction en pixel
EcY = Round(Me.Height - Me.InsideHeight) * PtPx 'ecart maximum toléré par la fonction en pixel
h1 = WindowXYFromPoint(x, y)
With Me
.StartUpPosition = 0
.Left = (x / PtPx * ActiveWindow.Zoom / 100) - 100 ' je deregle la position exemple je le met à -100 de left
.Top = (y / PtPx * ActiveWindow.Zoom / 100) + 100 ' je deregle la position exemple je le met à +10 de top
.Show 0
End With
'correction top
h2 = WindowXYFromPoint(x + EcX, y)
If h2 <> h1 Then
Do While h2 <> h1: Me.Top = Me.Top + 0.1: h2 = WindowXYFromPoint(x + EcX, y): Loop
Else
Do While h2 = h1: Me.Top = Me.Top - 0.1: h2 = WindowXYFromPoint(x + EcX, y): Loop 'on le repousse donc en bas
End If
'correction Left
h2 = WindowXYFromPoint(x, y + EcY)
If h2 <> h1 Then
Do While h2 <> h1: Me.Left = Me.Left + 0.1: h2 = WindowXYFromPoint(x, y + EcY): Loop
Else
Do While h2 = h1: Me.Left = Me.Left - 0.1: h2 = WindowXYFromPoint(x, y + EcY): Loop
End If
End Function
'////////////////////////////////////////////////////////////////////////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////////////////////////////
Private Sub UserForm_Activate()
If Not cel Is Nothing Then correction
End Sub