Hi All,
I'm hoping you may be able to assist.
I have a userform that when the user enters a date into textbox1, I'd like textbox2 to fill in with a date respective to the date in textbox1. I have this working in a normal excel sheet, but I'd like to remove formulas from the sheet and have them auto calculate in the userform. The formula I'm currently using is =A3+12-WEEKDAY(A3) this will always return the following Thursday of the next week.
Is anyone able to help?
Many thanks.
Kind Regards,
Quasis
Solved with:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'I used exit event, AfterUpdate is another option.
Dim myDays As Integer
If TextBox1.Value = vbNullString Then Exit Sub 'don't do anything if textbox1 is blank
If IsDate(TextBox1.Value) Then 'check whether textbox1 contains a date
myDays = 8 - Weekday(TextBox1.Value, vbThursday) 'find the difference in days between today and thursday
'next line checks if next thursday is this week or next, then adds 7 days if it is this week's thursday
If Int(((CDate(TextBox1.Value) - DateSerial(Year(CDate(TextBox1.Value)), 1, 0)) + 6) / 7) = Int(((DateAdd("d", myDays, TextBox1.Value) - DateSerial(Year(DateAdd("d", myDays, TextBox1.Value)), 1, 0)) + 6) / 7) Then myDays = myDays + 7
TextBox2.Value = DateAdd("d", myDays, TextBox1.Value)
End If
End Sub
Many thanks to Arkadi.
Bookmarks