Search code examples
vbabuttoncounter

Command button click counter to increase size of userform


I built a Userform consisting of 2 columns, each with 10 rows of text boxes. Users input and save information which later get pasted onto corresponding cells by clicking "save".

For convenience purposes, the default Userform_initialise is set with with specific measurements as to hide, with the help of a frame, the rows which are not currently used. If the user thinks he will need 5 rows, clicking on the button five time will edit these measurements to increase the size of the frame, unhiding the rows.

The problem is when the user completes more than one row, saves the userform, opens it again, and tries to add more rows. The userform will initialise showing the completed rows (e.g. 1-3), but clicking on "add line" to add additional rows will require 3 null clicks (as rows 1-3 are already unhidden), before the counter catches up and starts displaying additional rows as of the 4th click.

Screenshot screenshot

Private Sub commandbutton3_Click()

Static Counter As Integer

Counter = Counter + 1

If Counter = 1 Then
If Frame1.Height = 60 Then
UserForm.Height = 174
Frame1.Height = 90
CommandButton1.Top = 110
CommandButton2.Top = 110
commandbutton3.Top = 110
End If
End If

If Counter = 2 Then
If Frame1.Height = 90 Then
UserForm.Height = 204
Frame1.Height = 120
CommandButton1.Top = 140
CommandButton2.Top = 140
commandbutton3.Top = 140
End If
End If
Reset

If Counter = 3 Then
If Frame1.Height = 120 Then
UserForm.Height = 234
Frame1.Height = 150
CommandButton1.Top = 170
CommandButton2.Top = 170
commandbutton3.Top = 170
End If
End If
Reset

If Counter = 4 Then
If Frame1.Height = 150 Then
UserForm.Height = 264
Frame1.Height = 180
CommandButton1.Top = 200
CommandButton2.Top = 200
commandbutton3.Top = 200
End If
End If


If Counter = 5 Then
If Frame1.Height = 180 Then
UserForm.Height = 294
Frame1.Height = 210
CommandButton1.Top = 230
CommandButton2.Top = 230
commandbutton3.Top = 230
End If
End If


If Counter = 6 Then
If Frame1.Height = 210 Then
UserForm.Height = 324
Frame1.Height = 240
CommandButton1.Top = 260
CommandButton2.Top = 260
commandbutton3.Top = 260
End If
End If

If Counter = 7 Then
If Frame1.Height = 240 Then
UserForm.Height = 355
Frame1.Height = 270
CommandButton1.Top = 290
CommandButton2.Top = 290
commandbutton3.Top = 290
End If
End If

If Counter = 8 Then
If Frame1.Height = 270 Then
UserForm.Height = 382
Frame1.Height = 300
CommandButton1.Top = 320
CommandButton2.Top = 320
commandbutton3.Top = 320
End If
End If

If Counter = 9 Then
If Frame1.Height = 300 Then
UserForm.Height = 413
Frame1.Height = 330
CommandButton1.Top = 350
CommandButton2.Top = 350
commandbutton3.Top = 350
End If
End If

If Counter = 10 Then
If Frame1.Height = 330 Then
UserForm.Height = 564
Frame1.Height = 480
CommandButton1.Top = 500
CommandButton2.Top = 500
commandbutton3.Top = 500
End If
End If

If Counter > 10 Then
If Frame1.Height = 480 Then
MsgBox ("You have reached the maximum number of entries!"), vbEINFORMATION, "TRACKER"
End If
End If

Solution

  • For setting control/form position/height according to the value of Counter.

    My math may be be off, but you get the idea:

    Private Sub commandbutton3_Click()
    
        Static Counter As Integer, incr As Long
        
        Counter = Counter + 1
        incr = (Counter - 1) * 30
        
        UserForm.Height = 174 + incr
        Frame1.Height = 60 + incr
        CommandButton1.Top = 110 + incr
        CommandButton2.Top = CommandButton1.Top
        commandbutton3.Top = CommandButton1.Top
        
        If Counter > 10 Then
            MsgBox ("You have reached the maximum number of entries!"), vbInformation, "TRACKER"
        End If
    
    End If