将用户窗体与 Excel 中的单元格对齐
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我很抱歉让这个旧线程起死回生,但其他人对这个问题的回答要么不能完美工作,要么只能在特定情况下(全屏等)工作。
在许多论坛和其他相关问题上寻找这个问题的答案后,我在一个法国论坛上找到了一些简单的代码,它适用于 32 位版本的 excel (office 365)。
在模块中:
上面的代码可以轻松地修改为从
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:
The code above can be easily adapted to be launched from the
UserForm
by replacing theWith UserForm1
withWith Me
, and also removing the.Show
if needed (no point in trying to show itself since there are chances it is already on screen).