problem function date add into datepicker on userform

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
604
Office Version
2010
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
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
604
Office Version
2010
hi, dante the problem is not gives me right value in dtpicker2 only gives me right in one case it have to choose days or months or years separately
without collect lines code like this :
VBA Code:
Private Sub CommandButton2_Click()

    datepicker2 = DateAdd("d", textbox1.Text, datepicker1)

end  sub
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,901
Office Version
2010
Platform
Windows
What exactly are you supposed to be doing with the values in TextBox1, TextBox2 and TextBox3? Are you supposed to be adding each of those individually? In other words, let's assume you are starting today... are you trying to add one year to today, then add 17 months to the date you calculated by adding a year to today (in effect giving you 29 months from today and then on top of that adding another 517 days to that 29 months from now date?
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
11,688
Office Version
2007
Platform
Windows
Do you want to add days, months and years at the same time?

Could you put an example of what you have in datepicker1 and what you would expect as a result in datepicker2.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
11,688
Office Version
2007
Platform
Windows
If you have January 15, 2020
You want to add 1 year, 2 months and days, so the result would be March 18, 2021.
15 + 3 days = 18
January + 2 months = March
2020 + 1 year = 2021.
That is what you want?

Then:
Textbox1 = 3 (days)
Textbox2 = 2 (months)
Textbox3 = 1 (years)

Rich (BB code):
Private Sub CommandButton2_Click()
  datepicker2 = DateAdd("d", TextBox1.Value, datepicker1)
  datepicker2 = DateAdd("M", TextBox2.Value, datepicker2)
  datepicker2 = DateAdd("YYYY", TextBox3.Value, datepicker2)
End Sub
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
604
Office Version
2010
yes that what i ment dante but it gives me error mismatch 13
1.JPG
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
604
Office Version
2010
hi, rick i would add days and months and years to a new date as exactly as what said dante
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,901
Office Version
2010
Platform
Windows
Give this a try...
VBA Code:
Private Sub CommandButton2_Click()
  datepicker2 = DateAdd("d", TextBox1.Value, DateAdd("m", TextBox2.Value, DateAdd("yyyy", TextBox3.Value, datepicker1)))
End Sub
Note: You might have to specify the Value property for the datepicker1 control reference (I don't have a DatePicker control available, so I cannot check).
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
604
Office Version
2010
hi, rick you ignored datepicker2 this is not date and gives me the same error mismath 13
 

Watch MrExcel Video

Forum statistics

Threads
1,098,915
Messages
5,465,421
Members
406,426
Latest member
slari

This Week's Hot Topics

Top