Excel (VBA)

Excel VBAに関するフォーラムです。
  • 解決済みのトピックにはコメントできません。
このトピックは解決済みです。
質問

 
(Windows 10 Home : その他)
Userformの表示位置
投稿日時: 22/12/12 13:24:43
投稿者: o_taroh

ユーザーフォームをアクティブセルの近くに(上に)表示したいのですが、どのようにしてTOP、LEFTの値を得ればいいでしょうか?

回答
投稿日時: 22/12/12 13:56:40
投稿者: taitani
投稿者のウェブサイトに移動

Sub SelectionShow()
    Dim xAdd   As Long
    Dim yAdd   As Long
    Dim StrTmp As String
    Dim TagRng As Range
    Dim TagAdr As String
    
    StrTmp = Selection.Address
    If InStr(StrTmp, ":") = 0 Then
        TagAdr = StrTmp
    Else
        TagAdr = Mid(StrTmp, InStr(StrTmp, ":") + 1, Len(StrTmp) - InStr(StrTmp, ":"))
    End If
    
    Set TagRng = Range(TagAdr).Offset(1, 1)
    
    xAdd = ActiveWindow.PointsToScreenPixelsX(0) * 72 / 96 + TagRng.Left * ActiveWindow.Zoom / 100
    yAdd = ActiveWindow.PointsToScreenPixelsY(0) * 72 / 96 + TagRng.Top * ActiveWindow.Zoom / 100
    
    Load UserForm1
    
    With UserForm1
        .StartUpPosition = 0
        .Left = xAdd
        .Top = yAdd
    End With
    
    UserForm1.Show

End Sub

こんな感じでしょうか。
※微調整はお願いします。

回答
投稿日時: 22/12/12 14:06:56
投稿者: たかさん@富山

こんにちは。
 
Private Sub UserForm_Initialize()
    Dim dblX As Double, dblY As Double
    With ActiveWindow
        dblX = .PointsToScreenPixelsX(0) / 96 * 72 + ActiveCell.Left * .Zoom / 100
        dblY = .PointsToScreenPixelsY(0) / 96 * 72 + ActiveCell.Top * .Zoom / 100
    End With
    With Me
        .StartUpPosition = 0
        .Left = dblX
        .Top = dblY
    End With
End Sub

投稿日時: 22/12/12 15:19:08
投稿者: o_taroh

taitaniさん、たかさん@富山さんありがとうございました、実行し確認できました。
参考にさせていただき勉強頑張ります。