Textbox.value add days

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
834
Hello,

Trying to format textbox3 when a date is entered, working out whether it is a Monday/Tuesday/Wednesday/Thursday/Friday/Saturday or Sunday.

Then upon this calculation, I want to reformat the day of the week so that it adds 6 days if it is Monday (for example).

The first part works but the reformatting and adding doesn't quite.

Any ideas.

Thanks.


VBA Code:
Private Sub TextBox3_AfterUpdate()
    
Dim tDate As Date

If TextBox3.Text <> "" Then
    tDate = CDate(TextBox3.Text)
    If (TextBox4.Text = Format(tDate, "dddd") = "Monday") Then
    TextBox3.Value = Format(TextBox3.Value, "dd/mm/yyyy") + 6
End If
End If
    
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi,
try

VBA Code:
Private Sub TextBox3_AfterUpdate()
    Dim tDate As Date
    
    With Me.TextBox3
        If IsDate(.Value) Then
            tDate = DateValue(.Value)
            If Weekday(tDate) = vbMonday Then
                tDate = tDate + 6
                .Value = Format(tDate, "dd/mm/yyyy")
            End If
        End If
    End With
    
End Sub

Dave
 
Upvote 0
It is getting close, I need it to input into textbox4 so have adapted the code slightly. However this produces the following when I input 24/02/2020 (last Monday) and changes that too.

The top is textbox3 and below textbox4.

VBA Code:
    With Me.TextBox3
        If IsDate(.Value) Then
            tDate = DateValue(.Value)
            If Weekday(tDate) = vbMonday Then
                TextBox4.Value = tDate + 6
                .Value = Format(TextBox4.Value, "dd/mm/yyyy")
            End If
        End If
    End With
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    40.2 KB · Views: 2
Upvote 0
VBA Code:
I am getting closer!

Why does the date not like dd/mm/yyyy in textbox4?


With Me.TextBox3
        If IsDate(.Value) Then
            tDate = DateValue(.Value)
            If Weekday(tDate) = vbMonday Then
               TextBox4.Value = tDate + 6
                .Value = Format(TextBox3.Value, "dd/mm/yyyy")
            End If
        End If
    End With
    
End Sub

Private Sub TextBox4_AfterUpdate()

TextBox4.Value = Format(TextBox4.Value, "dd/mm/yyyy")

End Sub
 

Attachments

  • Untitled 2.png
    Untitled 2.png
    19.9 KB · Views: 1
Upvote 0
try

VBA Code:
Private Sub TextBox3_AfterUpdate()
    Dim tDate As Date
    
    With Me.TextBox3
        If IsDate(.Value) Then
            tDate = DateValue(.Value)
            If Weekday(tDate) = vbMonday Then
                tDate = tDate + 6
                Me.TextBox4.Value = Format(tDate, "dd/mm/yyyy")
            End If
        End If
    End With
    
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,144
Members
448,552
Latest member
WORKINGWITHNOLEADER

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