Search code examples
excelvbauserform

How to edit my userform code to allow for changes to update


I am working on an Excel userform and have all of the code working except for one piece. I will attach the code below but I'm hoping this is an easy fix. As you can see in my code, when the user enters the depart time and arrival time the data goes to my database and calculations are performed that are then sent back to my userform checkboxes. While this is working as needed, I have found that if the user makes a change in one of the times before leaving the userform, the information is updating appropriately in my database but the userform checkboxes aren't being updated. Can someone tell me what I need to do, if there is anything, to cause these to work in real time and change with my database document?

Private Sub txtDepartTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'When time is entered, time transfers immediately to spreadsheet time calculations datafield.

Dim TargetRow As Long
TargetRow = Sheets("Codes").Range("D43").Value + 1

If Not txtDepartTime Like "##:## [ap]m" Then
  MsgBox "Time entered is not valid.  Please enter time as hh:mm am/pm.", vbExclamation
  Cancel = True
  Exit Sub
End If

With Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 25)
 .Value = TimeValue(txtDepartTime)
 .NumberFormat = "hh:mm" 'departure time for checkbox calculation

 End With

End Sub

Private Sub txtArrivalTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'When time is entered, time transfers immediately to spreadsheet datafield and sends back to userform which meals are allowed.

Dim TargetRow As Long
TargetRow = Sheets("Codes").Range("D43").Value + 1

If Not txtArrivalTime Like "##:## [ap]m" Then
  MsgBox "Time entered is not valid.  Please enter time as hh:mm am/pm.", vbExclamation
  Cancel = True
  Exit Sub
End If

With Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 26)
 .Value = TimeValue(txtArrivalTime)
 .NumberFormat = "hh:mm" 'arrival time for checkbox calculation
End With

'''MEALS ALLOWED PER SPREADSHEET TO USERFORM'''
With Me.chkMorning
    If Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 28).Value = "T" Then
        .Value = Checked
    Else
        .Value = Unchecked
        .Enabled = False
    End If
End With

With Me.chkMidday
    If Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 30).Value = "T" Then
        .Value = Checked
    Else
        .Value = Unchecked
        .Enabled = False
    End If
End With

With Me.chkEvening
    If Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 32).Value = "T" Then
        .Value = Checked
    Else
        .Value = Unchecked
        .Enabled = False
    End If
End With
'''END MOVEMENT OF MEALS ALLOWED TO USERFORM'''

End Sub

Solution

  • Your code disables the checkboxes but doesn't re-enable them. Once disabled, their value can't be reset. I suggest this code instead of what you have.

    Dim Check As Boolean
    
    Check = (Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 28).Value = "T")
    With chkMorning
        .Enabled = vbTrue
        .Value = Abs(Check)
        .Enabled = .Value
    End With
    

    Now, by which method a particular cell in the referenced worksheet receives the value "T", or doesn't, I can't tell. I already suggested to look into sequencing. Given that a change made to a time would affect the cell being tested for the checkbox, that change could only take effect if Excel re-calculates the worksheet while VBA is running your userform. This I don't know. Therefore I wouldn't build on it.

    The basic scheme is - or should be - to read data into the form when it's opened (initialized), add, modify or manipulate these data in the form while the user looks at it, and hand back the additional, modified or manipulated data to the sheet when the user is done (with the form). Your concept doesn't follow this scheme. Therefore it creates problems you shouldn't have to face.