Hello,
I cannot get my head around the order of this code and I hope someone can help - I've tried to explain myself as clear as possible, but if you want to cut to the chase then the end of this question has the problem line of code..)!
I have a spreadsheet with a column called 'date of decs' (decs stands for 'declaration'). This can either be empty or have a date in it. The column header has a named range as 'Date_of_decs'.
When the user presses the 'menu' button to bring up a userform, the userform (amongst other things) contains a checkbox ('CB_date_of_decs') asking whether the date of declaration has been signed, and a textbox for the date.
The textbox ('TB_Date_of_Decs') is linked to the spreadsheet, like so (this is held in 'UserForm_Activate')
I want to be able to have the checkbox already checked if a date a filled out on the spreadsheet, and I am achieving this like so (this is also held in 'UserForm_Activate'): (This code also changes the colour of the textbox to grey or white, and later I will enable/disable it)
Finally, and this is where the issues arise, what I want to happen is if the user enables the checkbox, then the textbox is enabled (and turns white)
or if they disable the checkbox then:
1) The textbox is disabled;
2) If there are any dates in the textbox then the user is given a warning saying that the date will be deleted if they continue, however it shouldn't give this message if the textbox is already empty.
To do this here is my code:
The problem I have is that this code is firing alongside the 'UserForm_Activate' (which I guess would slow things down ever so slightly) and the bit 'If Not IsEmpty(TB_Date_of_Decs.Value) Then' doesn't seem to work - even if the textbox is empty it still fires the message box.
Can anyone shed some light into where I am going wrong? Thank you!
I cannot get my head around the order of this code and I hope someone can help - I've tried to explain myself as clear as possible, but if you want to cut to the chase then the end of this question has the problem line of code..)!
I have a spreadsheet with a column called 'date of decs' (decs stands for 'declaration'). This can either be empty or have a date in it. The column header has a named range as 'Date_of_decs'.
When the user presses the 'menu' button to bring up a userform, the userform (amongst other things) contains a checkbox ('CB_date_of_decs') asking whether the date of declaration has been signed, and a textbox for the date.
The textbox ('TB_Date_of_Decs') is linked to the spreadsheet, like so (this is held in 'UserForm_Activate')
VBA Code:
With ActiveCell
Date_of_Decs = ActiveSheet.Cells(.Row, Range("Date_of_Decs").Column)
TB_Date_of_Decs.Value = Date_of_Decs
End with
I want to be able to have the checkbox already checked if a date a filled out on the spreadsheet, and I am achieving this like so (this is also held in 'UserForm_Activate'): (This code also changes the colour of the textbox to grey or white, and later I will enable/disable it)
VBA Code:
With ActiveCell
If Not IsEmpty(ActiveSheet.Cells(.Row, Range("Date_of_Decs").Column).Value) Then
CB_date_of_decs.Value = True
TB_Date_of_Decs.BackColor = vbWhite
Else
CB_date_of_decs.Value = False
TB_Date_of_Decs.BackColor = &H80000010
End If
End With
Finally, and this is where the issues arise, what I want to happen is if the user enables the checkbox, then the textbox is enabled (and turns white)
or if they disable the checkbox then:
1) The textbox is disabled;
2) If there are any dates in the textbox then the user is given a warning saying that the date will be deleted if they continue, however it shouldn't give this message if the textbox is already empty.
To do this here is my code:
VBA Code:
Private Sub CB_date_of_decs_Click()
Dim response_decs As VbMsgBoxResult
If CB_date_of_decs.Value = True Then
TB_Date_of_Decs.BackColor = vbWhite
TB_Date_of_Decs.Enabled = True
Else
TB_Date_of_Decs.BackColor = &H80000010
TB_Date_of_Decs.Enabled = False
End If
If Not IsEmpty(TB_Date_of_Decs.Value) Then
response_decs = MsgBox("This will delete the date of decs entered", vbQuestion + vbOKCancel)
If response_decs = vbOK Then
TB_Date_of_Decs.Value = Empty
Else
CB_date_of_decs.Value = True
TB_Date_of_Decs.Enabled = True
Exit Sub
End If
End If
End Sub
The problem I have is that this code is firing alongside the 'UserForm_Activate' (which I guess would slow things down ever so slightly) and the bit 'If Not IsEmpty(TB_Date_of_Decs.Value) Then' doesn't seem to work - even if the textbox is empty it still fires the message box.
Can anyone shed some light into where I am going wrong? Thank you!