VBA running slow and causing Excel to freeze fro short time

DataZaan

New Member
Joined
Jan 22, 2019
Messages
1
Good day Everyone

Hope the week is treating everyone well so far.

I have a VBA code for a user form that is running extremely slow. The code is as follows:

Code:
Private Sub cmdaddentry_Click()
Dim Rowcount As Long
Dim ctl As Control
    If Me.txtshift.Value = "" Then
        MsgBox "Please enter a Shift.", vbExclamation, "WASA_datainput"
        Me.txtshift.SetFocus
        Exit Sub
    End If
    If Me.chkmachine.Value = "" Then
        MsgBox "Please enter a Machine.", vbExclamation, "WASA_datainput"
        Me.chkmachine.SetFocus
        Exit Sub
    End If
    If Me.txtdate.Value = "" Then
        MsgBox "Please enter a Date.", vbExclamation, "WASA_datainput"
        Me.txtdate.SetFocus
        Exit Sub
    End If
    If Me.txtshift.Value = "" Then
        MsgBox "Please enter a Shift.", vbExclamation, "WASA_datainput"
        Me.txtshift.SetFocus
        Exit Sub
    End If
    If Me.txtstart.Value = "" Then
        MsgBox "Please enter a Start.", vbExclamation, "WASA_datainput"
        Me.txtstart.SetFocus
        Exit Sub
    End If
    If Me.txtstop.Value = "" Then
        MsgBox "Please enter a Stop.", vbExclamation, "WASA_datainput"
        Me.txtstop.SetFocus
        Exit Sub
    End If
' Write data to worksheet
    Rowcount = Worksheets("Data capture").Range("A1").CurrentRegion.Rows.Count
    With Worksheets("Data capture").Range("A1")
        .Offset(Rowcount, 0).Value = Me.txtdate.Value
        .Offset(Rowcount, 1).Value = Me.txtshift.Value
        .Offset(Rowcount, 2).Value = Me.txtstart.Value
        .Offset(Rowcount, 3).Value = Me.txtstop.Value
        .Offset(Rowcount, 5).Value = Me.chkmachine.Value
        .Offset(Rowcount, 6).Value = Me.txtpops.Value
        .Offset(Rowcount, 7).Value = Me.txtbobbins.Value
        .Offset(Rowcount, 9).Value = Me.txtspools.Value
        .Offset(Rowcount, 10).Value = Me.txtcoils.Value
        .Offset(Rowcount, 11).Value = Me.txtkilograms.Value
        .Offset(Rowcount, 12).Value = Me.txtproduct.Value
        .Offset(Rowcount, 13).Value = Me.txttimestart.Value
        .Offset(Rowcount, 14).Value = Me.txttimestop.Value
        .Offset(Rowcount, 15).Value = Me.txtstrip.Value
    End With
' Clear the form
    For Each ctl In Me.Controls
        If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
            ctl.Value = ""
        End If
    Next ctl
End Sub


Private Sub cmdcancel_Click()
    Unload Me
End Sub


Private Sub cmdclear_Click()
' Clear the form
    For Each ctl In Me.Controls
        If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
            ctl.Value = ""
        End If
    Next ctl
End Sub

I have another form that runs super smoothly in the same workbook, the forms are not linked and reports to different sheets.

Any help would be appreciated.

Thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top