none
how to change userform size with command button RRS feed

  • Question

  • Hi

    this code not working.

    please help

    Private Sub CommandButton2_Click()
        If CommandButton2.Caption = "expand" Then
        If CommandButton2.Caption = "retract" Then
           UserForm1.Height = 455 = True
    Exit Sub
        End If
        End If
        If CommandButton2.Caption = " retract" Then
        If CommandButton2.Caption = " expand" Then
           UserForm1.Height = 96.75 = True
    Exit Sub
        End If
        End If
    End Sub

    regards

    Sunday, November 9, 2014 7:33 PM

Answers

  • Your End If statements are outside the Sub, so that's one error there. There's also a logical flaw. Look carefully at the code you've written. If the Caption value for the CommandButton2 is "expand", it goes in but the nested If condition is false so no statement is executed. I think below is what you want to do

    Private Sub CommandButton2_Click()
        If CommandButton2.Caption = "expand" Then
            UserForm1.Height = 455
            CommandButton2.Caption = "retract"
        Else
            UserForm1.Height = 97 ' Instead of 96.75
            CommandButton2.Caption = "expand"
        End If
    Exit Sub

    Hope it helped!

    Rahul

    • Marked as answer by dockhem2 Sunday, November 9, 2014 7:47 PM
    Sunday, November 9, 2014 7:38 PM

All replies

  • Your End If statements are outside the Sub, so that's one error there. There's also a logical flaw. Look carefully at the code you've written. If the Caption value for the CommandButton2 is "expand", it goes in but the nested If condition is false so no statement is executed. I think below is what you want to do

    Private Sub CommandButton2_Click()
        If CommandButton2.Caption = "expand" Then
            UserForm1.Height = 455
            CommandButton2.Caption = "retract"
        Else
            UserForm1.Height = 97 ' Instead of 96.75
            CommandButton2.Caption = "expand"
        End If
    Exit Sub

    Hope it helped!

    Rahul

    • Marked as answer by dockhem2 Sunday, November 9, 2014 7:47 PM
    Sunday, November 9, 2014 7:38 PM
  • Thank you Rahul y
    Sunday, November 9, 2014 7:47 PM