vba 将用户表单与 Excel 中的单元格对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6116929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:17:58  来源:igfitidea点击:

Align Userform to cells in Excel

excelexcel-vbauserformvba

提问by Dentist

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.

我试图在 Excel 中放置一个用户窗体(名为 UserForm1)以与 Excel 中的单元格引用对齐。如果我使用下面的第一个代码初始化表单,则用户表单的大小正确,位于 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

但是,我想调整大小以对齐用户窗体,使其覆盖单元格 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

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

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

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

考虑到屏幕分辨率/缩放大小,有没有办法正确地做到这一点?

Thanks Dentist

谢谢牙医

回答by user2296377

I stumbled across this link when I was looking for a solution to a similar problem. Since I have gained so much from this community, I thought I'd post a potential solution to the above question.

当我寻找类似问题的解决方案时,我偶然发现了这个链接。由于我从这个社区中获得了很多,我想我会针对上述问题发布一个潜在的解决方案。

The below article might prove useful:

以下文章可能有用:

http://www.cpearson.com/excel/formposition.htm

http://www.cpearson.com/excel/formposition.htm

Mr. Pearson has an excellent site filled with tips, tricks, and tools, plus generally good information.

Mr. Pearson 有一个很好的网站,里面有很多提示、技巧和工具,还有很好的信息。