【问题标题】:Problem in VBA userforms - resolution totally changedVBA 用户表单中的问题 - 分辨率完全改变
【发布时间】:2021-07-09 17:20:27
【问题描述】:

我在 VBA 中构建了一个包含多个用户表单的 excel 文件,其中包含按钮(表单控件)。通常我使用扩展坞将我的笔记本电脑连接到大屏幕(我的办公室)。有时我在没有扩展坞的情况下打开文件(只有笔记本电脑)。 我今天打开文件,发现分辨率完全改变了,用户表单变得如此之大,并且在所有按钮中,里面的文本都向右换行。 请您的支持知道我该如何解决这个问题?

谢谢

【问题讨论】:

  • 请查看How to Askminimal reproducible example 并相应更新您的问题。
  • 你知道这些是form control还是active x按钮?另外,这是在工作表上还是表格上?
  • @Kubi 我编辑了我的答案,VBA 中的用户表单改变了他们的分辨率

标签: excel vba


【解决方案1】:

这是由于在您的本机屏幕分辨率更改时打开文件引起的问题(并且可能仅在这也更改纵横比时) - 将笔记本电脑连接或断开连接到/的最常见原因从外部屏幕(在这种情况下,通过您的扩展坞)

此问题有两种形式:按钮保持相同大小,但内容(文本、图像等)在左上角锚定时按比例放大/缩小 - 这就是这里发生的情况 -或者内容保持相同大小,但按钮本身会变大/变小,直到它覆盖整个工作表或太小而无法单击。

根据我的经验,修复按钮的唯一方法是调整它们,并强制 Excel 重新绘制形状,而不是“记住”它应该是什么样子。您可以手动执行此操作,但我会尝试找到一些代码来为您“重置”按钮。 这里有一些代码可以为您做事。

(对于用户窗体,您可能只需调用 Me.Repaint 即可强制重绘,而无需费心调整大小 - 但我尚未对此进行测试,因为我永远无法做到这一点按需发生的问题>_

用户窗体按钮修复

Sub FixButtonFormat(ByRef Button As Control)
    Dim Top As Double, Left As Double, Width As Double, Height As Double, FontName As String, FontSize As Double

    Top = Button.Top
    Left = Button.Left
    Width = Button.Width
    Height = Button.Height
    FontName = Button.Object.Font.Name
    FontSize = Button.Object.Font.Size

    'Scale Button up slightly
    Button.Top = Top - 1
    Button.Left = Left + 1
    Button.Width = Width - 2
    Button.Height = Height + 2
    Button.Object.Font.Size = FontSize + 1

    DoEvents
    UserForm1.Repaint
    DoEvents

    'Reset button to original size
    Button.Top = Top
    Button.Left = Left
    Button.Width = Width
    Button.Height = Height
    Button.Object.Font.Name = FontName
    Button.Object.Font.Size = FontSize
End Sub

工作表按钮修复

Sub FixButtonFormat(ByRef Button As Shape)
    If Button.Type <> msoFormControl And Button.Type <> msoOLEControlObject Then Exit Sub
    Dim Top As Double, Left As Double, Width As Double, Height As Double, FontName As String, FontSize As Double
    Dim Screen As Boolean
    Screen = Application.ScreenUpdating

    Top = Button.Top
    Left = Button.Left
    Width = Button.Width
    Height = Button.Height
    If Button.Type = msoFormControl Then 'Form Control
        FontName = Button.OLEFormat.Object.Font.Name
        FontSize = Button.OLEFormat.Object.Font.Size
    ElseIf Button.Type = msoOLEControlObject Then 'ActiveX Control
        FontName = Button.DrawingObject.Object.Font.Name
        FontSize = Button.DrawingObject.Object.Font.Size
    End If

    'Scale Button up slightly
    Button.Top = Top - 1
    Button.Left = Left + 1
    Button.Width = Width - 2
    Button.Height = Height + 2
    If Button.Type = msoFormControl Then 'Form Control
        Button.OLEFormat.Object.Font.Size = FontSize + 1
    ElseIf Button.Type = msoOLEControlObject Then 'ActiveX Control
        Button.DrawingObject.Object.Font.Size = FontSize + 1
    End If

    If Not Screen Then
        Application.ScreenUpdating = True
        DoEvents
        Application.ScreenUpdating = False
    Else
        DoEvents
    End If

    'Reset button to original size
    Button.Top = Top
    Button.Left = Left
    Button.Width = Width
    Button.Height = Height
    If Button.Type = msoFormControl Then 'Form Control
        Button.OLEFormat.Object.Font.Name = FontName
        Button.OLEFormat.Object.Font.Size = FontSize
    ElseIf Button.Type = msoOLEControlObject Then 'ActiveX Control
        Button.DrawingObject.Object.Font.Size = FontSize
        Button.DrawingObject.Object.Font.Name = FontName
    End If
End Sub

【讨论】:

  • 非常感谢!
【解决方案2】:

在 Office365 中,我遇到了同样的问题(我一定是在外接显示器上显示 VBA 表单时将笔记本电脑与扩展坞分离了),所有内容都变得超大。

我进入代码并打开表单对象以手动调整所有内容 - 当我单击 UserForm 矩形的右下角来调整它的大小时,表单上的所有控件都会立即将自身纠正回适当的大小以匹配到我当前的屏幕分辨率,然后回到表单的左上角。我所要做的就是减少用户窗体以匹配更正的控件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    相关资源
    最近更新 更多