Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Excel Button resizes after clicking on it (Command Button)

How can I stop a button from resizing? Each time I click on the button, either the size of the button or the font size changes.

Note: I cannot lock my sheet as my Macro will write into the sheet.

Autosize is turned off. I run Excel 2007 on Windows 7 (64 Bit).

like image 823
HHeckner Avatar asked Mar 06 '12 09:03

HHeckner


People also ask

How do you stop Excel buttons from moving?

Right mouse over the button, select Format Autoshape and on the Properties tab select Don't move or size with cells. You must log in or register to reply here.

How do I click a command button in VBA?

VBA ActiveX CommandButton Control on the Worksheet Go To Developer Tab and then click Insert from the Controls group. Click on the Command Button from the ActiveX Controls group. Drag a Command Button on the Worksheet. Right click on the Command Button, before that Design Mode should be turned ON.

How do I resize a macro button?

Right-click on the controls and select "Format Control..." Navigate to the "Properties" tab. Make sure that the option "Don't move or size with cells" is selected.


5 Answers

I use the following for ListBoxes. Same principle for buttons; adapt as appropriate.

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

This uses the following type and procedures:

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
like image 175
Jean-François Corbett Avatar answered Oct 07 '22 19:10

Jean-François Corbett


I added some code to the end of the onClick thus:

CommandButton1.Width = 150
CommandButton1.Height = 33
CommandButton1.Font.Size = 11

Seems to work.

I got the issue a slightly different way. By opening the workbook on my primary laptop display, then moving it to my big monitor. Same root cause I would assume.

like image 42
TObyFish Avatar answered Oct 07 '22 17:10

TObyFish


Seen this issue in Excel 2007, 2010 and 2013

This code prevents the issue from manifesting. Code needs to run every time a active X object is activated.

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
like image 27
user6645884 Avatar answered Oct 07 '22 18:10

user6645884


(Excel 2003)

It seems to me there are two different issues: - resizing of text of ONE button when clicking on it(though not always, don't know why), and - changing the size of ALL buttons, when opening the workbook on a display with a different resolution (which subsist even when back on the initial display).

As for the individual resizing issue: I found that it is sufficient to modify one dimension of the button to "rejuvenate" it. Such as :

 myButton.Height = myButton.Height + 1
 myButton.Height = myButton.Height - 1

You can put it in each button's clicking sub ("myButton_Click"), or implement it a custom Classe for the "onClick" event.

like image 42
Aeggie78 Avatar answered Oct 07 '22 18:10

Aeggie78


I experienced the same problem with ActiveX buttons and spins in Excel resizing and moving. This was a shared spreadsheet used on several different PC's laptops and screens. As it was shared I couldn't use macros to automatically reposition and resize in code.

In the end after searching for a solution and trying every possible setting of buttons. I found that grouping the buttons solved the problem immediately. The controls, buttons, spinners all stay in place. I've tested this for a week and no problems. Just select the controls, right click and group - worked like magic.

like image 38
Michael Madigan Avatar answered Oct 07 '22 19:10

Michael Madigan