problem function date add into datepicker on userform

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,430
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi
i have problem add years and months days is ok i have textbox1= days textbox2= months textbox3= years when i choose in datepicker1= date then add months an years to datepicker2
this is my simple code
VBA Code:
Private Sub CommandButton2_Click()
    datepicker2 = DateAdd("d", textbox1.Text, datepicker1)
     datepicker2 = DateAdd("M", textbox2.Text, datepicker1)
      datepicker2 = DateAdd("YYYY", textbox3.Text, datepicker1)
End Sub


1.JPG
 
about your question yes textbox3= days yes now it works but not completely the years is wrong it supposes 6/01/2022 not 6/01/2026
ا3.JPG
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Are your years, months and days TextBoxes supposed to represent the same time period? In other words, are you entering 744 into the days TextBox and you have code showing what that number would be in months or (not and) years? If so, then all you have to do is add it to the start date (with days, you do not need to use DateAdd as VBA knows how to add days to dates directly)...

datepicker2 =datepicker1 + TextBox1.Value

where I am guessing TextBox1 is your "days" TextBox (if I guessed wrong, then use the correct TextBox in place of my TextBox1)
 
Upvote 0
i can say 744 entering into the days and 24 into months and 2 into years so the days and months and years are separated each them
 
Upvote 0
I am sorry, but I do not understand what you are trying to say. Let try something different. How did the numbers 744, 24 and 2 get into your TextBoxes? Did you type each one individually? Or do you have code filling them in for you somehow?
 
Upvote 0
i have this code fill them
VBA Code:
Private Sub CommandButton1_Click()
Dim strD As Date
Dim endD As Date
Dim diff, DIFF2, DIFF3 As Integer
strD = TextBox1.Value
endD = TextBox2.Value
diff = DateDiff("D", strD, endD)
diff1 = DateDiff("M", strD, endD)
DIFF2 = DateDiff("yyyy", strD, endD)
Me.TextBox3.Value = diff
Me.TextBox4.Value = diff1
Me.TextBox5.Value = DIFF2 
end sub
 
Upvote 0
Okay, that is kind of what I thought. Did you try the single code line I posted in Message #22 yet? It should add the correct amount of time to datepicker1 and give you the date you want in datepicker2.
 
Upvote 0
it's unbelievable, rick this simple line code works perfectly , it causes headache for 3 days to do that and it takes many hours on the internet .i don't find anything , thanks rick so much but i have a simple question why my original code doesn't work by use dateadd and dante's code barly works , so it is useless function ,do you have any idea ?
 
Upvote 0
hi rick i face another problem about the days in datediff i know no why it supposes = 15 days not 440
1.JPG
 
Upvote 0
the days and a new dat are wrong
1.JPG










ا3.JPG


2.JPG



the right should this
4.JPG

5.JPG
 

Attachments

  • 1.JPG
    1.JPG
    40.7 KB · Views: 3
  • 3.JPG
    3.JPG
    44.3 KB · Views: 3
  • 3.JPG
    3.JPG
    44.3 KB · Views: 2
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,695
Members
449,331
Latest member
smckenzie2016

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