Compare textbox userform date with today's date

sofas

Active Member
Joined
Sep 11, 2022
Messages
468
Office Version
  1. 2019
Platform
  1. Windows
Hello. I want to put an icon in the userform button to compare dates. Example.. Comparing the date in the text box with today's date, if it is smaller, the text box is colored red, and if the difference is 30 days or less, it is colored
Yellow. And other than that he does nothing
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I want to put an icon in the userform button
That implies you want a picture to be on/in a command button. What does that have to do with
the text box is colored red, and if the difference is 30 days or less, it is colored Yellow.
Also, this is not clear...
if the difference is 30 days or less,
Both of these dates - 5/05/23 and 3/08/23 - differ from today's date (Apr. 06, 2023) by 30 days or less. So does it matter if the supplied date is in the past or the future?

Also, if the logic applies to dates that are both in the past and future, then both red and yellow conditions can be true? That means if the date is smaller by 30 days, it meets both conditions, so then what?
 
Upvote 0
That implies you want a picture to be on/in a command button. What does that have to do with

Also, this is not clear...

Both of these dates - 5/05/23 and 3/08/23 - differ from today's date (Apr. 06, 2023) by 30 days or less. So does it matter if the supplied date is in the past or the future?

Also, if the logic applies to dates that are both in the past and future, then both red and yellow conditions can be true? That means if the date is smaller by 30 days, it meets both conditions, so then what?
Hello.I just want to know the correct way to do it.I will put the code in Private Sub Listbox_Click()

To clarify: I have textbox1 the date is fetched from the worksheet and I have textbox2 that includes today's date I want when checking the date in textbox 1 is smaller than the date of textbox 2 the color is changed to red and when checking that there are 30 days or less left for the future date the color is changed to yellow. If no condition is met, remain white
 
Upvote 0
OK, I just don't have the patience to guess at which control is the "future date", when or which one might be less than the other and so on. No idea what event you want to use to trigger the code either. You can play with this if you want and see if you can make it work, or use it as a basis for what you need.
VBA Code:
Dim ctl1 As control, ctl2 As control
Set ctl1 = Me.TextBox1
Set ctl2 = Me.TextBox2

If IsDate(ctl1) And IsDate(ctl2) Then
     Select Case True
          Case ctl1 < ctl2
               ctl1.BackColor = vbRed
          Case ctl1 > ctl2 And ctl1 < DateDiff("d", ctl2, ctl1) < 31
               ctl1.BackColor = vbYellow
          Case Else
               ctl1.BackColor = vbWhite
     End Select
End If

Set ctl1 = Nothing
Set ctl2 = Nothing
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,816
Members
449,049
Latest member
cybersurfer5000

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