problem function date add into datepicker on userform

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
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
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
yes that what i ment dante but it gives me error mismatch 13
1.JPG
 
Upvote 0
hi, rick i would add days and months and years to a new date as exactly as what said dante
 
Upvote 0
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).
 
Upvote 0
hi, rick you ignored datepicker2 this is not date and gives me the same error mismath 13
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,729
Members
448,294
Latest member
jmjmjmjmjmjm

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