How do I stop a userform from filling a row?

hoof_arted

New Member
Joined
Jan 31, 2017
Messages
5
I'm well experienced with Excel but I'm fairly new to VBA. I have a userform that will enter values into the next empty row when the "Enter" button is clicked. The issue I'm having is that, after the next empty row is filled, if the user hits the enter button again it fills the next row with the same values. Is there a way to stop this? Maybe by clearing the userform on enter click? My code is a bit cumbersome, but it does what I want except for that one issue. If anyone sees how I could lighten my code a bit, I'm all ears. Code is as follows:

Code:
Private Sub cmdEnter_Click()

Dim ws1 As Worksheet, ws2 As Worksheet
Dim Found As Range, Found2 As Range
Dim Search As String
Dim iRow As Long
Set ws1 = Worksheets("PWCDaily(1st)")
Set ws2 = Worksheets("PWCDaily(2nd)")
iRow = ws1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
iRow2 = ws1.Cells(Rows.Count, 8).End(xlUp).Offset(1, 0).Row
iRow3 = ws2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
iRow4 = ws2.Cells(Rows.Count, 8).End(xlUp).Offset(1, 0).Row
Search = Me.MetricsDTPick.Value
Set Found = ws1.Columns(1).Find(Search, LookIn:=xlValues, LookAt:=xlWhole)
Set Found2 = ws2.Columns(1).Find(Search, LookIn:=xlValues, LookAt:=xlWhole)

If Found Is Nothing And _
    Me.shiftCombo.Value = "1st" And _
    Me.pwcCombo.Value = "601" Then
    Do
        ws1.Activate
        Cells(iRow, 1).Value = Me.MetricsDTPick.Value
        Cells(iRow, 2).Value = Me.schedTxt.Value
        Cells(iRow, 3).Value = Me.actualTxt.Value
        Cells(iRow, 4).Value = Me.compSeriesTxt.Value
    Exit Do
    Loop While Found Is Nothing
    ElseIf Not Found Is Nothing And _
        Me.shiftCombo.Value = "1st" And _
        Me.pwcCombo.Value = "601" Then
        Do
            ws1.Activate
            Cells(iRow, 2).Value = Me.schedTxt.Value
            Cells(iRow, 3).Value = Me.actualTxt.Value
            Cells(iRow, 4).Value = Me.compSeriesTxt.Value
        Exit Do
        Loop While Not Found Is Nothing
        
    ElseIf Found Is Nothing And _
        Me.shiftCombo.Value = "1st" And _
        Me.pwcCombo.Value = "721" Then
        Do
            ws1.Activate
            Cells(iRow, 1).Value = Me.MetricsDTPick.Value
            Cells(iRow2, 8).Value = Me.schedTxt.Value
            Cells(iRow2, 9).Value = Me.actualTxt.Value
            Cells(iRow2, 10).Value = Me.compSeriesTxt.Value
        Exit Do
        Loop While Found Is Nothing
        
    ElseIf Not Found Is Nothing And _
        Me.shiftCombo.Value = "1st" And _
        Me.pwcCombo.Value = "721" Then
        Do
            ws1.Activate
            Cells(iRow2, 8).Value = Me.schedTxt.Value
            Cells(iRow2, 9).Value = Me.actualTxt.Value
            Cells(iRow2, 10).Value = Me.compSeriesTxt.Value
        Exit Do
        Loop While Not Found Is Nothing
    
    ElseIf Found2 Is Nothing And _
        Me.shiftCombo.Value = "2nd" And _
        Me.pwcCombo.Value = "601" Then
        Do
            ws2.Activate
            Cells(iRow3, 1).Value = Me.MetricsDTPick
            Cells(iRow3, 2).Value = Me.schedTxt.Value
            Cells(iRow3, 3).Value = Me.actualTxt.Value
            Cells(iRow3, 4).Value = Me.compSeriesTxt.Value
        Exit Do
        Loop While Found2 Is Nothing
    
    ElseIf Not Found2 Is Nothing And _
        Me.shiftCombo.Value = "2nd" And _
        Me.pwcCombo.Value = "601" Then
        Do
            ws2.Activate
            Cells(iRow3, 2).Value = Me.schedTxt.Value
            Cells(iRow3, 3).Value = Me.actualTxt.Value
            Cells(iRow3, 4).Value = Me.compSeriesTxt.Value
        Exit Do
        Loop While Not Found2 Is Nothing
    
    ElseIf Found2 Is Nothing And _
        Me.shiftCombo.Value = "2nd" And _
        Me.pwcCombo.Value = "721" Then
        Do
            ws2.Activate
            Cells(iRow3, 1).Value = Me.MetricsDTPick.Value
            Cells(iRow3, 8).Value = Me.schedTxt.Value
            Cells(iRow3, 9).Value = Me.actualTxt.Value
            Cells(iRow3, 10).Value = Me.compSeriesTxt.Value
        Exit Do
        Loop While Found2 Is Nothing
        
    ElseIf Not Found2 Is Nothing And _
        Me.shiftCombo.Value = "2nd" And _
        Me.pwcCombo.Value = "721" Then
        Do
            ws2.Activate
            Cells(iRow4, 8).Value = Me.schedTxt.Value
            Cells(iRow4, 9).Value = Me.actualTxt.Value
            Cells(iRow4, 10).Value = Me.compSeriesTxt.Value
        Exit Do
        Loop While Not Found2 Is Nothing
            
End If     
        
End Sub

Thanks in advance for any help.;)
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
So when your script has run you want all the textboxes in your userform cleared is that what you want.
 
Upvote 0
Have you tried unloading / reloading the form....add this at the end of your script

Code:
Application.ScreenUpdating = False 
Unload Me 
userform1.show "change userform name if required
Application.ScreenUpdating = True
 
Upvote 0
If your wanting to clear all the Textboxes add these lines of code to the bottom of your script:

Code:
For Each xControl In Me.Controls
        If TypeName(xControl) = "TextBox" Then xControl.Value = vbNullString
    Next xControl
 
Upvote 0
If the OP has a lot of TB's my code might be a quicker option...:LOL:
 
Upvote 0
Awesome! This worked! Thank you. Because of the way I had to write the loop, if the userform wasn't cleared and the user hit the enter button a second time the data would carry over to the next empty row. This did the trick.
 
Upvote 0
Michael M, I tried the unload and reload trick before. It didn't do what I was hoping. Thanks for the reply!
 
Upvote 0
Glad I was able to help you. Come back here to Mr. Excel next time you need additional assistance.
Awesome! This worked! Thank you. Because of the way I had to write the loop, if the userform wasn't cleared and the user hit the enter button a second time the data would carry over to the next empty row. This did the trick.
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,618
Members
449,238
Latest member
wcbyers

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