Userform enter function disabled after opening and updating external file

03856me

Active Member
Joined
Apr 4, 2008
Messages
297
I have written the following code with works as expected when the user presses the Add button. The data from the userform is added to the table within the workbook and then an external workbook is opened and specific textbox values are added to a table in that workbook, then it is saved and closed then the focus is back on the userform in the current workbook after clearing the textboxes.

This works perfectly the first time but then when it comes back to the userform to add another record the "Enter" function or "Tab" function to move through the textboxes won't work. At this point all I can do is click using my mouse through the fields or close the form and launch it again. Could you take a look at my code and see what I need to change to this will stop happening.....many thanks.

Code:
Private Sub cmd_add_Click()
    Dim answer As String
    With txt_hrsran
        If txt_hrsran.Value <> txt_hrstotal.Value Then
            answer = MsgBox("Your Hours Actually Ran amount must equal the Total Hours amount for the products" & _
                    vbNewLine & "Please modify your input", vbOKOnly + vbCritical, "ERROR")
        Exit Sub
        End If
     End With
    
    If txt_total.Value > "" Then AddData
    If txt_total.Value > "" Then AddDataDashboard
    ClearData
End Sub

'============================================================
'MACRO TO ADD DATA
'============================================================
Private Sub AddData()     ' adds data to this database
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("data")
iRow = ws.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
    ws.Cells(iRow, 3).Value = Me.txt_date.Value
    ws.Cells(iRow, 4).Value = "South"
    ws.Cells(iRow, 5).Value = "District 1"
    ws.Cells(iRow, 6).Value = Me.cbo_shift.Value
    
End Sub

Private Sub AddDataDashboard()     ' adds data to the dashboard
    Application.ScreenUpdating = False
    On Error Resume Next
    Workbooks("workbook1.xlsm").Activate
    If Err <> 0 Then Err.Clear: Workbooks.Open Filename:= _
        "C:\Users\Documents\workbook1.xlsm", _
        WriteResPassword:="12345"

Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("records")
iRow = ws.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
    ws.Cells(iRow, 3).Value = Me.txt_date.Value
    ws.Cells(iRow, 5).Value = "South"
    ws.Cells(iRow, 6).Value = Me.txt_hrsran.Value
    ws.Cells(iRow, 7).Value = Me.txt_hrssch.Value
    
    Windows("workbook1.xlsm").Activate
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    Windows("OriginalWorkbook.xlsm").Activate   'Brings focus back to userform
    
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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