单击VBA Excel按钮后调整大小(命令按钮)

use*_*338 14 excel vba resize button

如何停止按钮调整大小?每次单击按钮时,按钮的大小或字体大小都会发生变化.

注意:我无法锁定我的工作表,因为我的宏将写入工作表.

自动调整大小已关闭.我在Windows 7(64位)上运行Excel 2007.

Jea*_*ett 6

我对ListBoxes使用以下内容.按钮的原理相同; 适当适应.

Private Sub myButton_Click()
    Dim lb As MSForms.ListBox
    Set lb = Sheet1.myListBox

    Dim oldSize As ListBoxSizeType
    oldSize = GetListBoxSize(lb)

    ' Do stuff that makes listbox misbehave and change size.
    ' Now restore the original size:
    SetListBoxSize lb, oldSize
End Sub
Run Code Online (Sandbox Code Playgroud)

这使用以下类型和过程:

Type ListBoxSizeType
    height As Single
    width As Single
End Type

Function GetListBoxSize(lb As MSForms.ListBox) As ListBoxSizeType
    GetListBoxSize.height = lb.height
    GetListBoxSize.width = lb.width
End Function

Sub SetListBoxSize(lb As MSForms.ListBox, lbs As ListBoxSizeType)
    lb.height = lbs.height
    lb.width = lbs.width
End Sub
Run Code Online (Sandbox Code Playgroud)


小智 6

我在最后添加了一些代码onClick

CommandButton1.Width = 150
CommandButton1.Height = 33
CommandButton1.Font.Size = 11
Run Code Online (Sandbox Code Playgroud)

似乎工作。

我以稍微不同的方式解决了这个问题。通过在我的主要笔记本电脑显示器上打开工作簿,然后将其移动到我的大显示器上。我会假设相同的根本原因。


小智 5

在 Excel 2007、2010 和 2013 中看到此问题

此代码可防止问题显现。每次激活活动 X 对象时都需要运行代码。

Sub Shared_ObjectReset()

    Dim MyShapes As OLEObjects
    Dim ObjectSelected As OLEObject

    Dim ObjectSelected_Height As Double
    Dim ObjectSelected_Top As Double
    Dim ObjectSelected_Left As Double
    Dim ObjectSelected_Width As Double
    Dim ObjectSelected_FontSize As Single

    ActiveWindow.Zoom = 100

    'OLE Programmatic Identifiers for Commandbuttons = Forms.CommandButton.1
    Set MyShapes = ActiveSheet.OLEObjects
    For Each ObjectSelected In MyShapes
        'Remove this line if fixing active object other than buttons
        If ObjectSelected.progID = "Forms.CommandButton.1" Then
            ObjectSelected_Height = ObjectSelected.Height
            ObjectSelected_Top = ObjectSelected.Top
            ObjectSelected_Left = ObjectSelected.Left
            ObjectSelected_Width = ObjectSelected.Width
            ObjectSelected_FontSize = ObjectSelected.Object.FontSize

            ObjectSelected.Placement = 3

            ObjectSelected.Height = ObjectSelected_Height + 1
            ObjectSelected.Top = ObjectSelected_Top + 1
            ObjectSelected.Left = ObjectSelected_Left + 1
            ObjectSelected.Width = ObjectSelected_Width + 1
            ObjectSelected.Object.FontSize = ObjectSelected_FontSize + 1

            ObjectSelected.Height = ObjectSelected_Height
            ObjectSelected.Top = ObjectSelected_Top
            ObjectSelected.Left = ObjectSelected_Left
            ObjectSelected.Width = ObjectSelected_Width
            ObjectSelected.Object.FontSize = ObjectSelected_FontSize

        End If
    Next

End Sub
Run Code Online (Sandbox Code Playgroud)


SWa*_*SWa 0

使用 Forms 按钮而不是 ActiveX 按钮,ActiveX 控件在工作表上会随机出现异常行为