将用户窗体与 Excel 中的单元格对齐

发布于 2024-11-09 05:02:54 字数 635 浏览 0 评论 0原文

我试图在 Excel 中定位用户窗体(名为 UserForm1),以与 Excel 中的单元格引用对齐。如果我使用下面的第一个代码初始化表单,则用户表单的大小正确,位于 Excel 屏幕的中心。

Private Sub UserForm_Initialize()

With Me
  .Width = 200
  .Height = 170
End With

End Sub

但是,我想调整大小以对齐用户表单,使其覆盖单元格 B3:D15。我已经尝试过

Private Sub UserForm_Initialize()

With Me
.Top = Sheet1.Range("B3").Top
.Left = Sheet1.Range("B3").Left
.Width = Sheet1.Range("B3").Offset(0, 4).Left - Sheet1.Range("B3").Left
.Height = Sheet1.Range("B3").Offset(12, 0).Top - Sheet1.Range("B3").Top
End With

End Sub

,但用户表单出现在功能区上(Excel 缩放为 100%。)

有没有办法在考虑屏幕分辨率/缩放大小的情况下正确执行此操作?

I'm attempting to position a userform (named UserForm1) in Excel to align with cell references in Excel. If I initialize the form using the first code below, the userform is correct size positioned in the centre of the screen in Excel.

Private Sub UserForm_Initialize()

With Me
  .Width = 200
  .Height = 170
End With

End Sub

However, I want to resize to align the userform so that it covers cells B3:D15. I've tried

Private Sub UserForm_Initialize()

With Me
.Top = Sheet1.Range("B3").Top
.Left = Sheet1.Range("B3").Left
.Width = Sheet1.Range("B3").Offset(0, 4).Left - Sheet1.Range("B3").Left
.Height = Sheet1.Range("B3").Offset(12, 0).Top - Sheet1.Range("B3").Top
End With

End Sub

but the userform appears over the ribbon (Excel zoom is at 100%.)

Is there a way to do this properly taking into account screen resolution/zoom size?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

今天小雨转甜 2024-11-16 05:02:54

首先,我很抱歉让这个旧线程起死回生,但其他人对这个问题的回答要么不能完美工作,要么只能在特定情况下(全屏等)工作。
在许多论坛和其他相关问题上寻找这个问题的答案后,我在一个法国论坛上找到了一些简单的代码,它适用于 32 位版本的 excel (office 365)。

在模块中:

Option Explicit
 
Private Declare Function GetDC& Lib _
"user32.dll" (ByVal hwnd&)
Private Declare Function GetDeviceCaps& _
Lib "gdi32" (ByVal hDC&, ByVal nIndex&)
 
Sub UserFormAlign()
    ' 1 inch = 72 points for usually 96 or 120 dpi
    Dim x#, y#, w#, h#
    x = GetDeviceCaps(GetDC(0), 88) / 72
    y = GetDeviceCaps(GetDC(0), 90) / 72
    With UserForm1
        .StartUpPosition = 0
        .Left = (ActiveWindow.PointsToScreenPixelsX(ActiveCell.Left * x) * 1 / x) + ActiveCell.Width
        .Top = (ActiveWindow.PointsToScreenPixelsY(ActiveCell.Top * y) * 1 / y) + ActiveCell.Height
        .Show 'if not already shown
    End With
End Sub

上面的代码可以轻松地修改为从 UserForm 启动,方法是将 With UserForm1 替换为 With Me,并删除如果需要的话,.Show(尝试显示自己没有意义,因为它很可能已经在屏幕上)。

First I apologize for bringing back this old thread from the dead, but others answers for this problem on SO either doesn't work perfectly or only on specific situations (fullscreen, etc.).
After looking for answers to this questions on many forums and others SO related questions, I found some simple code on a french forum that works just fine for 32bits version of excel (office 365).

In a module:

Option Explicit
 
Private Declare Function GetDC& Lib _
"user32.dll" (ByVal hwnd&)
Private Declare Function GetDeviceCaps& _
Lib "gdi32" (ByVal hDC&, ByVal nIndex&)
 
Sub UserFormAlign()
    ' 1 inch = 72 points for usually 96 or 120 dpi
    Dim x#, y#, w#, h#
    x = GetDeviceCaps(GetDC(0), 88) / 72
    y = GetDeviceCaps(GetDC(0), 90) / 72
    With UserForm1
        .StartUpPosition = 0
        .Left = (ActiveWindow.PointsToScreenPixelsX(ActiveCell.Left * x) * 1 / x) + ActiveCell.Width
        .Top = (ActiveWindow.PointsToScreenPixelsY(ActiveCell.Top * y) * 1 / y) + ActiveCell.Height
        .Show 'if not already shown
    End With
End Sub

The code above can be easily adapted to be launched from the UserForm by replacing the With UserForm1 with With Me, and also removing the .Show if needed (no point in trying to show itself since there are chances it is already on screen).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文