以下で使用される API 呼び出しを含むこの投稿を見つけました。Application.Commandbars("Ribbon").Height でリボンの高さを取得できることも思い出しました。したがって、VBA では次のようにします。
編集: フォーミュラ バーと見出しの高さの問題に対応して、それらを非表示にし、ActiveWindow.Height を取得してから表示し、新しい ActiveWindow.Height を取得して違いを表す関数を追加しました。その関数は、変換前に高さを合計する以下の行で呼び出されます。うまくいくと思いますが、多くのテストは行っていません。
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Const LOGPIXELSX As Long = 88
Private Const LOGPIXELSY As Long = 90
Sub CellTopLeftPixels(rng As Excel.Range)
Dim RibbonHeight As Long
Dim TotalTop As Long
Dim TotalLeft As Long
RibbonHeight = Application.CommandBars("Ribbon").Height
TotalTop = (RibbonHeight + GetFormulaBarAndHeadingsHeight + rng.Top) * PixelsPerPointY
TotalLeft = rng.Left * PixelsPerPointX
Debug.Print "Top: "; TotalTop; " Left: "; TotalLeft
End Sub
Function GetFormulaBarAndHeadingsHeight()
Dim ActiveWindowHeightWhenHidden As Long
Dim ActiveWindowHeightWhenShown As Long
Application.DisplayFormulaBar = False
ActiveWindow.DisplayHeadings = False
ActiveWindowHeightWhenHidden = ActiveWindow.Height
Application.DisplayFormulaBar = True
ActiveWindow.DisplayHeadings = True
ActiveWindowHeightWhenShown = ActiveWindow.Height
GetFormulaBarAndHeadingsHeight = ActiveWindowHeightWhenHidden - ActiveWindowHeightWhenShown
End Function
Function PixelsPerPointX() As Double
Dim hdc As Long
Dim PixPerInchX As Long
hdc = GetDC(0)
PixPerInchX = GetDeviceCaps(hdc, LOGPIXELSX)
PixelsPerPointX = PixPerInchX / 72
ReleaseDC 0, hdc
End Function
Function PixelsPerPointY() As Double
Dim hdc As Long
Dim PixPerInchY As Long
hdc = GetDC(0)
PixPerInchY = GetDeviceCaps(hdc, LOGPIXELSY)
PixelsPerPointY = PixPerInchY / 72
ReleaseDC 0, hdc
End Function
上記の 72 は 1 インチあたりのポイント数です。
次のように呼び出します。
Sub test()
CellTopLeftPixels ActiveCell
End Sub